Drjacky / ImagePicker

📸Image Picker for Android, Pick images from Gallery or Capture a new image with Camera🖼
https://github.com/Drjacky/ImagePicker
Apache License 2.0
230 stars 57 forks source link

NullPointerException in callback from "ActivityResult.data?.data!!" when it's picking the image from Google Photos #86

Closed fernandoperez0330 closed 1 year ago

fernandoperez0330 commented 1 year ago

The URI from callback in the gallery launcher is returning null when it's picking the image from Google Photos. Making the app crashing after the image being cropped. To be more specific, it's the line to get the URI (it.data?.data!!) in the code below:

private val launcher =
    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        if (it.resultCode == Activity.RESULT_OK) {
            // THIS IS THE CODE THAT IT'S CRASHING
            val uri = it.data?.data!!
            // Use the uri to load the image
       }
   }

How to Reproduce? 1- Pick the photo from gallery. 2- Select Google Photos

3- Pick a photo from the google photo gallery.

4- Crop the image and tap the tick icon at the right top.

Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=252738298, result=-1, data=Intent { (has extras) }} to activity ACTIVITY}: java.lang.NullPointerException
       at android.app.ActivityThread.deliverResults(ActivityThread.java:5782)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:5823)
       at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2613)
       at android.os.Handler.dispatchMessage(Handler.java:110)
       at android.os.Looper.loop(Looper.java:219)
       at android.app.ActivityThread.main(ActivityThread.java:8668)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
Caused by java.lang.NullPointerException:
       at ACTIVITY.galleryLauncher$lambda-1(ACTIVITY.java:LINE)
       at com.github.drjacky.imagepicker.ImagePickerActivity$$InternalSyntheticLambda$0$b81bb44912f5b4a0271a70d74f4ba93f52c894bfeec5341f534b29298bbf2ba5$0.onActivityResult$bridge(ImagePickerActivity.java:18)
       at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:418)
       at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:375)
       at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:777)
       at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:152)
       at android.app.Activity.dispatchActivityResult(Activity.java:8541)
       at android.app.ActivityThread.deliverResults(ActivityThread.java:5775)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:5823)
       at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2613)
       at android.os.Handler.dispatchMessage(Handler.java:110)
       at android.os.Looper.loop(Looper.java:219)
       at android.app.ActivityThread.main(ActivityThread.java:8668)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)

Here's the configuration that the app is using for the imagePicker:

ImagePicker.with(this)
            .maxResultSize(1080, 1080)
            .crop()
            .galleryOnly()
            .cropOval()
            .crop(x = 1f, y= 1f)

Devices affected (6)

Device Brand:Google Model:Pixel 7 Operating System Version:Android 13 File Permission: Yes

=========================

Device Brand:FIH Model:AT&T Motivate™ 2 Operating System Version:Android 11 File Permission: Yes

=========================

Device Brand:HUAWEI Model:P20 Pro Operating System Version: Android 10 File Permission: Yes

fernandoperez0330 commented 1 year ago

For what it's worth, this code stops the crashing and im able now to get the photo but don't know if this it's a integral solution here.

Screenshot 2022-12-13 at 11 27 02 a m

ClarkXP commented 1 year ago

I'm facing the same issue on Samsung S21 Ultra, recently upgraded to Android 13, up to android 12 still works

Drjacky commented 1 year ago

I had to downgrade from ACTION_GET_CONTENT to ACTION_OPEN_DOCUMENT due to these: https://github.com/Yalantis/uCrop/issues/857 & https://github.com/Drjacky/ImagePicker/issues/77

But anyway, please try this new version: 2.3.21

Catlandor commented 1 year ago

Related to #88.

I actually fixed this issue, but my changes to the GalleryProvider were not included. See: https://github.com/Catlandor/ImagePicker/commit/2af4c3d11d8103505c1dfa5acdceeeec48705700#diff-b2b473b09e781f64ffe811773512b42c506a13e8c77f8bc6f0ddf97c4bbb89f2

Drjacky commented 1 year ago

Fixed on 2.3.22. Thanks to @Catlandor 🎉