Android-Image-Cropper: [BUG] - Unable to Crop image from Uri

  • Lib Version [e.g. 3.0.0]

Describe the bug First of all I would like to thank everyone involved in this library for the great work you have done. Well done.

I am currently using this library in a project where I have a personalized interface where the user can take a photo. When he takes the picture I take this uri and send it to another screen to edit the image. On this screen I have a button to crop the image, which is where I call the CropImage.

This is the contract:

fun createCropImageContract(uri: Uri): ActivityResultContract<Any?, Uri?> {
            return object : ActivityResultContract<Any?, Uri?>() {
                override fun createIntent(context: Context, input: Any?): Intent {
                    return CropImage
                        .activity(uri)
                        .getIntent(context)
                }

                override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
                    return CropImage.getActivityResult(intent)?.uriContent
                }
            }
        }

However, when I call the method, the CropImage’s Activity is called and is immediately closed returning the Uri as null.

This is what I was able to see in the log (I don’t know if it’s related but it appears every time I press the button) :

2021-04-14 09:48:08.723 3211-3211/? E/[IndicatorGarden]Presenter: onGardenApplyWindowInsets() New DisplayCutout is NULL!!
2021-04-14 09:48:08.725 3211-3211/? E/SystemUIImageView: set Image Drawable!!
2021-04-14 09:48:08.725 3211-3211/? E/SystemUIImageView: set Background Drawable!!
2021-04-14 09:48:08.729 3211-3211/? E/[IndicatorGarden]Presenter: onGardenApplyWindowInsets() New DisplayCutout is NULL!!
2021-04-14 09:48:08.786 3211-3211/? E/[IndicatorGarden]Presenter: onGardenApplyWindowInsets() New DisplayCutout is NULL!!
2021-04-14 09:48:08.788 3211-3211/? E/SystemUIImageView: set Image Drawable!!
2021-04-14 09:48:08.788 3211-3211/? E/SystemUIImageView: set Background Drawable!!
2021-04-14 09:48:08.792 3211-3211/? E/[IndicatorGarden]Presenter: onGardenApplyWindowInsets() New DisplayCutout is NULL!!

I have another feature which is the creation of a profile, there I call this contract and everything is working correctly:

fun createCropImageContract(): ActivityResultContract<Any?, Uri?> {
            return object : ActivityResultContract<Any?, Uri?>() {
                    override fun createIntent(context: Context, input: Any?): Intent {
                        return CropImage
                            .activity()
                            .getIntent(context)
                    }

                    override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
                        return CropImage.getActivityResult(intent)?.uriContent
                    }
                }
        }

The problem is only when I already send Uri directly. Am I doing something wrong?

To Reproduce Call CropImage Passing the Uri from fragment:

CropImage
                        .activity(uri)
                        .getIntent(context)
                }

Expected behavior Open the CropImage activity to crop the provided Uri

Screenshots If applicable, add screenshots to help explain your problem.

Smartphone (please complete the following information):

  • Pixel 2 XL Android 11
  • Samsung Galaxy S9+ Android 10

Additional context

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

@Canato Okay I think I can reproduce it because I had the issue in the past. I will look into it after a few days😊

Hi @Canato, I think this is enough, thank!

I try to use like the Glide library, want to load file path from getUriFilePath() into ImageView

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.INSTANCE.getActivityResult(data);
            if (resultCode == Activity.RESULT_OK) {

// Is work
//                Uri resultUri = result.getUriContent();
//                Glide.with(this).load(resultUri).into(imageView); 

// Are the first cropped pictures
                File file = new File(result.getUriFilePath(this));
                Glide.with(this).load(file).into(imageView);
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {

            }
        }
    }

Sample video

Hi,

Use library com.github.CanHub:Android-Image-Cropper:2.3.1 for the same case as from in Android above 10.

Google Pixel 4 Android 11:

Uri uri = result.getUri();
url => content://com.package.cropper.fileprovider/my_images/Pictures/cropped1948079140939926657.jpg

Hey @Taogo-Hugo this behaviour is correctly. Please use the latest version of the library so you can use getUriContent and getUriFilePath

Hi,

Use library com.github.CanHub:Android-Image-Cropper:2.3.1 for the same case as from in Android above 10.

Google Pixel 4 Android 11:

Uri uri = result.getUri();
url => content://com.package.cropper.fileprovider/my_images/Pictures/cropped1948079140939926657.jpg