Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - Unable to Crop image from Uri #114

Closed
felipecs81 opened this issue Apr 14, 2021 · 17 comments
Closed

[BUG] - Unable to Crop image from Uri #114

felipecs81 opened this issue Apr 14, 2021 · 17 comments

Comments

@felipecs81
Copy link

  • 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

@felipecs81 felipecs81 added the bug label Apr 14, 2021
@Canato
Copy link
Member

Canato commented Apr 14, 2021

Hey @felipecs81 thanks for raising this.

This maybe be some issue with the URI path. Could you log your URI?
Since is Android 11, I may be expecting some content:// instead of file:// in the URI.

If this is the case, we maybe need to do some changes how we consume de URI.
In the mean time I have some hack possibility for you, but let's check what is the value first.

Another thing that can help, if you can reproduce on the library samples we have here will be very easier to debug for me hahahah

@Canato Canato added the pinned label Apr 14, 2021
@felipecs81
Copy link
Author

Hey @Canato Thank you for you quick reply.
Sorry to answer only now, I think we are in different timezones.

This is the URI which I am passing to the cropper:
/storage/emulated/0/Android/data/au.com.wave.app/files/Pictures/1618445175986_picture.jpg

This URI is created using the method: Uri.parse(args.filePath)

Due to the architecture, I can only access the URI through the path.

@Canato
Copy link
Member

Canato commented Apr 15, 2021

@felipecs81 do you have the READ_EXTERNALSTORAGE permissions granted, right?

This look like a path issue, because of Android 11. Can you try on Android 9? So we can be sure.

If this is the case, is something in your URI, if not we keep digging. But for sure we need a more clear error message when this happens.

Keep me on loop please

@Taogo-Hugo
Copy link

Taogo-Hugo commented Apr 27, 2021

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

@Taogo-Hugo
Copy link

Hi,

Equipment environment:Google Pixel 4 Android 11

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

uri.getPath(): /my_images/Pictures/cropped6350359284020855240.jpg

i tried url convert file method by link that could work.

correct file path: /data/user/0/com.package/cache/1619505580451.jpg

@Canato
Copy link
Member

Canato commented Apr 27, 2021

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

@Canato
Copy link
Member

Canato commented Apr 27, 2021

Hi,

Equipment environment:Google Pixel 4 Android 11

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

uri.getPath(): /my_images/Pictures/cropped6350359284020855240.jpg

i tried url convert file method by link that could work.

The Uri is correct here.

correct file path: /data/user/0/com.package/cache/1619505580451.jpg

This is not the correct file path because of the Android 10 and 11 storage scope changes based on external permission.

If after using the latest version you still find issues, please open a PR with a reproducible sample code so I can debug =)
Sadly I cannot reproduce it

@Taogo-Hugo

@Taogo-Hugo
Copy link

Hi Canato,

Great! I already use the latest version[3.0.1] and getUriFilePath() method of Kotlin project that could work.

I want to consult with you,

Crop image A:
uri: content://com.package.cropper.fileprovider/my_images/Pictures/cropped8381537426439084171.jpg, path:/data/user/0/com.package/cache/temp_file.jpg

Crop image B:
uri: content://com.package.cropper.fileprovider/my_images/Pictures/cropped2248782402080642280.jpg, path:/data/user/0/com.package/cache/temp_file.jpg

then crop image for getUriFilePath() is "temp_file.jpg" unique file?

@Canato
Copy link
Member

Canato commented Apr 28, 2021

indeed @Taogo-Hugo if you want to keep this file you need to copy and save.

This is done so the library would not take too much cache space.

If you feel that you need different files for each image we can do \o/!
Just open a PR with the suggestion where getUriFilePath() receive an optional parameter to make it unique. Where the default behaviour os override. Make sense?

If you really need, maybe I can do it for next week

@Taogo-Hugo
Copy link

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

@Canato
Copy link
Member

Canato commented Apr 29, 2021

@Taogo-Hugo interesting hahahah, we need to finish one loading before bring the next one. Will add later the change

#122

@Taogo-Hugo
Copy link

Hi @Canato, Ok, thanks.

@Canato
Copy link
Member

Canato commented May 15, 2021

Removing the pinned tag cause I didn't got some answers =/

@Canato Canato removed the pinned label May 15, 2021
@ashique-perumbavoor
Copy link
Contributor

I just wanted to know weather the issue is solved or not. If not please let me know I will look into it.

@Canato
Copy link
Member

Canato commented May 19, 2021

@ashique-perumbavoor thanks for the interest, I'm not sure if was solved or not, cause no one answer me anymore
😢

Basically this is related to the URI path, now in the library we have two methods to get the URI, one return content and another file when the user need as file.

Until now no one raise new issues about this, but since I could never reproduce the error my self I'm not sure if is really fixed.

Ideally someone who can reproduce could provide a sample with the error or fix it.

Please feel free to dig a little see if you can reproduce and drop a PR =D

@ashique-perumbavoor
Copy link
Contributor

@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😊

@stale
Copy link

stale bot commented Jun 2, 2021

Let's keep this mess organised! This issue has been automatically marked as stale because it has not had recent activity. =( It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 2, 2021
@stale stale bot closed this as completed Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants