Yalantis / uCrop

Image Cropping Library for Android
https://yalantis.com/blog/introducing-ucrop-our-own-image-cropping-library-for-android/
11.89k stars 2.16k forks source link

Black images on Android 13 #873

Open TeamRainless opened 1 year ago

TeamRainless commented 1 year ago

Do you want to request a feature or report a bug?

report a bug

What is the current behavior?

Since updating to Android 13, seemingly randomly, images will appear completely black after upload. Sometimes uCrop simply crashes. I suspect this is because of "startactivityForResult" being deprecated and all the insane hurdles everyone has had to go through in order to get uCrop to work without it. I suspect it might've been completely removed from 13.

What is the expected behavior?

Not black image.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

Here's my solution to get uCrop running:

ActivityResultLauncher<Intent> ucropActivityIntent = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                public void onActivityResult(ActivityResult result) {
                    if (result.getData() != null && result.getResultCode() == RESULT_OK) {

                        Uri selectedUri = result.getData().getData();
                        String selectedUri2 = result.getData().getData().getPath();
                        getMimeType(getApplicationContext(), selectedUri);

                        startCropActivity(selectedUri);

                    }
                }
            });

Please attach any image files, URL and stack trace that can be used to reproduce the bug.

Which versions of uCrop, and which Android API versions are affected by this issue? Did this work in previous versions of uCrop?

Latest uCrop and the Latest API.

jens-muenker commented 1 year ago

I guess nobody with write access updates this library anymore. (https://github.com/Yalantis/uCrop/pull/651#issuecomment-757772589)

I made a fork which is working well for Android 13. In addition to the small updates, I translated most of the code to kotlin.

TeamRainless commented 1 year ago

I guess nobody with write access updates this library anymore. (#651 (comment))

I made a fork which is working well for Android 13. In addition to the small updates, I translated most of the code to kotlin.

Excellent! And thanks for the reply. Do you have an example on implementation? I mean you say it works like uCrop in the ReadMe... but it obviously can't if you're using ActivityForResult now...

EDIT2: It actually looks like it's still using "startActivityForResult" in the example from a week ago...

jens-muenker commented 1 year ago

Yes you're right, ActivityForResult still exists, but I added the option to pass an ActivityResultLauncher to the start function. I wrote the following function in my personal project to use uCrop:

fun startCrop(uri: Uri, aspectRatio: AspectRatio, context: Context, activityResultLauncher: ActivityResultLauncher<Intent>) {

        Files.createDirectories(File(context.cacheDir.toString() + "/createRecipeTempImages/").toPath())

        var uCrop = of(uri, Uri.fromFile(File(context.cacheDir.toString() + "/createRecipeTempImages/",  currentTimeMillis().toString() + ".jpg")))
        //var uCrop = of(uri, Uri.fromFile(File(context.cacheDir, UriUtils.instance.getFileNameWithoutEndingFromUri(context, uri) + ".jpg")))
        uCrop = uCrop.withAspectRatio(aspectRatio.aspectRatioX, aspectRatio.aspectRatioY)

        uCrop.start(context, activityResultLauncher)
    }

I hope this helps and I would improve the documentation with examples in the next weeks. I know there still exist some things which are marked as deprecated. I plan to replace these in the future.

If you have any questions left, feel free to ask me anytime :)

EDIT: I added an example to the README