failed to call intent (camera.action.CROP) on AVD(API Level 31) without any exception msg

86 Views Asked by At

My code ia as below.

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
intent.setData(mImageCaptureUri);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
     grantUriPermission(getPackageName(),mImageCaptureUri,Intent.FLAG_GRANT_READ_URI_PERMISSION);
     grantUriPermission(getPackageName(),mImageCropUri,Intent.FLAG_GRANT_READ_URI_PERMISSION);
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
    intent.putExtra("outputX", 480);
    intent.putExtra("outputY", 480);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);

    intent.putExtra("scale", true);
    intent.putExtra("scaleUpIfNeeded", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCropUri);

              intent.putExtra("crop", "true");
              intent.putExtra("return-data", false);
              intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
              intent.putExtra("noFaceDetection", true);

try {
      startActivityForResult(intent, CROP_FROM_CAMERA);
    }
    catch (Exception e) {
                e.printStackTrace();
    }
  1. The test environment is AVD(API Level 31). It should have a default crop app, Google Photo. And my code about taking photo works fine.

  2. When running my app, I select a .jpg file from Downloads folder, and then call intent camera.action.CROP). But the code doesn't take effect at all.

  3. I checked the two uri in logcat,

mImageCaptureUri = content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F71/ORIGINAL/NONE/image%2Fpng/1987893059

mImageCropUri = content://com.myapp.fileprovider/my_pic/Pictures/tmp_kuilimar_16712596169354514375851720661420.jpg

In addition, I can't get any exception message in LogCat with e.printStackTrace().

So, what's wrong? ...... Thank you all in advance.

2

There are 2 best solutions below

1
CommonsWare On

Android does not have a CROP Intent.

It should have a default crop app, Google Photo

There is no requirement for any device, let alone any user of any device, to have an app that supports your undocumented and unsupported Intent action. And there is no requirement for Google Photo, or any other particular app, to support that undocumented and unsupported Intent action.

There are many image cropping libraries available for Android. Please use one.

0
Johnson On

I found that the issue resulted from the grantUriPermission(). We should grant Uri Permission to Google Photo, which will preform CROP, other than the app self. Thank you all.