react-native-image-picker: App crash after clicking 'Take photo' (Android)

Description

Hello,

Some Android devices (samsung A5, Android 7.0) crash just after clicking “Take photo” in the dialog box. (Choose picture from library works well.)

Please find the crash logs:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'boolean com.facebook.react.bridge.ReadableMap.hasKey(java.lang.String)' on a null object reference
       at com.imagepicker.ImagePickerModule.parseOptions(ImagePickerModule.java:707)
       at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:233)
       at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:206)
       at com.imagepicker.ImagePickerModule$2.onTakePhoto(ImagePickerModule.java:162)
       at com.imagepicker.utils.UI$1.onClick(UI.java:54)
       at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:807)
       at android.widget.AdapterView.performItemClick(AdapterView.java:343)
       at android.widget.AbsListView.performItemClick(AbsListView.java:1664)
       at android.widget.AbsListView$PerformClick.run(AbsListView.java:4075)
       at android.widget.AbsListView$10.run(AbsListView.java:6557)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
       at java.lang.Thread.run(Thread.java:762)

How to repeat issue and example

Here is the source code I use to open the image picker dialog:

const photoOptions = {
      title: '',
      cancelButtonTitle: 'Annuler',
      takePhotoButtonTitle: 'Prendre une photo...',
      chooseFromLibraryButtonTitle: 'Choisir une photo...',
 };

showImagePicker(photoOptions, response => {
      if (!response.didCancel && !response.error && !response.customButton) {
        const imageSource = {
          uri: response.uri,
          name: response.fileName ? response.fileName : 'image.JPG',
          type: 'image/jpg',
        };

        this.setState({ imageSource });
      }
 });

Solution

The null pointer appears here in the image-picker source code (ImagePickerModule.java):

private void parseOptions(final ReadableMap options) {
  noData = false;
  if (options.hasKey("noData")) {    **<==== HERE, options is null, but I can figure out why !?**
    noData = options.getBoolean("noData");
  }
  imageConfig = imageConfig.updateFromOptions(options);
  pickVideo = false;
  if (options.hasKey("mediaType") && options.getString("mediaType").equals("video")) {
    pickVideo = true;
  }
  videoQuality = 1;
  if (options.hasKey("videoQuality") && options.getString("videoQuality").equals("low")) {
    videoQuality = 0;
  }
  videoDurationLimit = 0;
  if (options.hasKey("durationLimit")) {
    videoDurationLimit = options.getInt("durationLimit");
  }
}

Is it possible to add a NPE check on options variable ?

Thanks.

Additional Information

  • React Native version: 0.54.2
  • React Native Image Picker version: 0.26.7
  • Platform: Android
  • Development Operating System: MacOS High Sierra 10.13.4
  • Dev tools: Android Studio

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 25

Most upvoted comments

Any solution with this issue? I’m getting this is some devices.

Same. react-native-image-picker 1.0.2 . RN 0.60.4 .Only in Redmi 7, other devices including other Redmis work fine. It’s weird. In my case, the app restarts to splash screen without showing any error.

Edit: I created new issue #1149

@appli-intramuros : btw, add one line <uses-feature android:name="android.permission.BLUETOOTH" />, that’s what I found also

+1

This solution didn’t apply for me, but can help someone else.

Go to Settings > Developer options. Scroll Down till you find “Don’t keep activities”. Usually it should be under “App” heading. Disable it IF it’s enabled. (If its disabled, then this solution is not for you) Try running the file upload now. It should work.