Cropping an image when shooting with a camera

Below is my sister code snippet , where it redirects to the cropping app device after the picture was taken from the camera. I usually follow this example . But during the phase / moment between the shot taken from the camera and what is available in the crop app, it shows a constant download and makes my app terribly unresponsive. If this happens at least once, every time I open the application, it just shows the download.

This problem does not occur in one scenario → When I do not move my device (i.e. only in the absence of screen orientation). Can someone please suggest me to avoid this problem?

 public void shotFromCamera(View view) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri); try { intent.putExtra("return-data", true); startActivityForResult(intent, PICK_FROM_CAMERA); } catch (ActivityNotFoundException e) { e.printStackTrace(); } } protected void onActivityResult(int requestCode, int resultCode,Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode==RESULT_OK && requestCode == PICK_FROM_CAMERA){ doCrop(); } else if(resultCode == RESULT_OK && requestCode == CROP_FROM_CAMERA) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photoBitmap = extras.getParcelable("data"); imageView.setImageBitmap(photoBitmap); if(mImageCaptureUri != null) { File f = new File(mImageCaptureUri.getPath()); if (f.exists()) f.delete(); } mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+ "/MyApp", "avatar" + String.valueOf(System.currentTimeMillis()) + ".jpg")); File file = new File(mImageCaptureUri.getPath()); selectedImagePath = mImageCaptureUri.getPath(); Log.d("pic1 ","pic to be created: "+selectedImagePath); OutputStream fOut = null; try { fOut = new FileOutputStream(file); photoBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); fOut.flush(); fOut.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } } } private void doCrop() { final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setType("image/*"); List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 ); int size = list.size(); if (size == 0) { Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show(); return; } else { intent.setData(imageCaptureUri); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("scale", true); intent.putExtra("return-data", true); if (size == 1) { Intent i = new Intent(intent); ResolveInfo res = list.get(0); i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); startActivityForResult(i, CROP_FROM_CAMERA); } } 

To clarify my problem, I ran the source code in this example, as was the case with the same question. I followed a couple more examples that redirect to the cropping application. The same problem occurs when the orientation changes during the phase described above.

PS . If someone has a working example that certainly works even with a change in orientation, I would be happy to accept it as an answer.

+6
source share
2 answers

You can go with it -

  • get the bitmap from the specified URI, i.e. a full-sized image using

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(imageCaptureUri));

  • Use Bitmap.CompressFormat.PNG when compressing an image

  • create a scaled bitmap using

    photo= Bitmap.createScaledBitmap(background, YourWidth, YourHeight, true);

  • This is your scaled (cropped) image.

+1
source

check the value of imageCaptureUri! = null before the line intent.setData(imageCaptureUri); in doCrop()

Edit

First save the camera result in a file and transfer the file to the cropping application

 intent.setData(Uri.fromFile(new File(path))); 
0
source

Source: https://habr.com/ru/post/950597/


All Articles