react-native-image-crop-picker icon indicating copy to clipboard operation
react-native-image-crop-picker copied to clipboard

"User cancelled image selection" error when call openPicker

Open kurisu994 opened this issue 3 years ago • 3 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

It solved a problem that throw "User cancelled image selection" error when call openPicker method on Vivo devices

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
index c6b09a9..33bab98 100644
--- a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
+++ b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
@@ -372,8 +372,8 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
             galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);
             galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
 
-            final Intent chooserIntent = Intent.createChooser(galleryIntent, "Pick an image");
-            activity.startActivityForResult(chooserIntent, IMAGE_PICKER_REQUEST);
+//            final Intent chooserIntent = Intent.createChooser(galleryIntent, "Pick an image");
+            activity.startActivityForResult(galleryIntent, IMAGE_PICKER_REQUEST);
         } catch (Exception e) {
             resultCollector.notifyProblem(E_FAILED_TO_SHOW_PICKER, e);
         }

This issue body was partially generated by patch-package.

kurisu994 avatar Aug 20 '22 03:08 kurisu994

Hi, thank you for your help and attempt to solve the issue, @kurisu994.

I did the same and it did not solve the issue.

  1. I installed patch-package lib with "yarn add patch-package postinstall-postinstall";
  2. I added the script to package.json:
"scripts": {
+  "postinstall": "patch-package"
 }
  1. I made the fixes you made;
  2. I created the patch with "yarn patch-package react-native-image-crop-picker" (v0.38.0);
  3. I applied the patch running "yarn patch-package";
  4. I ran "yarn android" and it did not solve the problem.

Brenosalv avatar Aug 24 '22 00:08 Brenosalv

I solved the problem with try/catch:

import ImagePicker from 'react-native-image-crop-picker';

const getImage = async () => {
  try {
    const image = await ImagePicker.openPicker({
      width: 300,
      height: 400,
      cropping: true
    });

    const result = { uri: image.path }

    return result;
  } catch (error) {
    console.warn(error);
  }
}

export default getImage;

This way, it does not crashes when the user cancel the picker. We just need to ignore the warning.

Brenosalv avatar Aug 24 '22 01:08 Brenosalv

@Brenosalv greeting! In my project android/app/src/main/AndroidManifest.xml MainActivity use launchMode="singleTop". Maybe there's a difference.

kurisu994 avatar Aug 24 '22 03:08 kurisu994

这个方案在 0.37.3 版本上可行,在出现问题的 Vivo 手机上测试可以打开相册,其他手机例如小米、魅族、OPPO也是没有问题的。

This solution is feasible on version 0.37.3, and the photo album can be opened by testing on the Vivo mobile phone with problems. Other mobile phones such as Xiaomi, Meizu, and OPPO also have no problems.

onlyling avatar Sep 27 '22 02:09 onlyling