react-native-image-picker: Camera doesn't open and error "Couldn't get file path for photo" is reported

Bug

After upgrading react-native-image-picker from version 1.1.0 to 2.0.0, the launchCamera method returns the error: “Couldn’t get file path for photo”. This happens only when the WRITE_EXTERNAL_STORAGE permission was not yet given by the user. After the user gave permissions for WRITE_EXTERNAL_STORAGE and the Photo Library was opened, I can open the camera without any problems.

Since version 2.0.0 introduced splitting the permissions on Android, I expect that the camera still needs WRITE_EXTERNAL_STORAGE permissions to open (at least on the Android Simulator).

I’ve tried using all suggestions given in other issues. These suggestions mainly point at adding paths to app/src/main/res/xml/filePaths.xml, but all these things don’t help.

Be aware that I tested this situation in the Android Simulator (see below for specific version)

Environment info

React native info output:

System:
    OS: macOS 10.15.2
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 114.09 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.13.1 - ~/.nvm/versions/node/v12.13.1/bin/node
    Yarn: 1.19.1 - /usr/local/bin/yarn
    npm: 6.12.1 - ~/.nvm/versions/node/v12.13.1/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 23, 25, 26, 27, 28, 29
      Build Tools: 27.0.3, 28.0.3, 29.0.1, 29.0.2
      System Images: android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5900203
    Xcode: 11.3/11C29 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.12.0 => 16.12.0
    react-native: 0.61.5 => 0.61.5

Library version: 2.0.0

Android simulator running on Nexus 6P, API version 28, x86 CPU.

Steps To Reproduce

This is only an Android issue:

  1. Make sure to revoke all permissions that were previously given.

  2. Open the app

  3. From your app, invoke launchCamera with these options:

{
    quality: 1.0,
    allowsEditing: false,
    title: global.i18n._(t`Select a picture`),
    cancelButtonTitle: global.i18n._(t`Cancel`),
    takePhotoButtonTitle: global.i18n._(t`Take a picture…`),
    chooseFromLibraryButtonTitle: global.i18n._(t`Choose a picture…`),
    permissionDenied: {
      title: global.i18n._(t`Access denied`),
      text: global.i18n._(t`To grant access to your camera and pictures.`),
      reTryTitle: global.i18n._(t`try again`),
      okTitle: global.i18n._(t`I'm sure`),
    },
  }
  1. See permission popup asking for permission to access your Camera

  2. Give permission

  3. See “Couldn’t get file path for photo” error being reported

Describe what you expected to happen:

I expect the camera to be opened

Reproducible sample code

See above. I use the following Android configuration in my build configuration:

    ext {
        buildToolsVersion = "29.0.2"
        minSdkVersion = 21
        compileSdkVersion = 29
        targetSdkVersion = 29
        androidMapsUtilsVersion = "0.5+"
        supportLibVersion = "29.0.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath('com.android.tools.build:gradle:3.5.1')
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

        // Firebase Push Notifications
        classpath 'com.google.gms:google-services:4.3.2'
    }

ADB Logcat output

01-15 13:46:34.939 21875 22498 W System.err: java.io.IOException: Permission denied
01-15 13:46:34.940 21875 22498 W System.err: 	at java.io.UnixFileSystem.createFileExclusively0(Native Method)
01-15 13:46:34.941 21875 22498 W System.err: 	at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
01-15 13:46:34.941 21875 22498 W System.err: 	at java.io.File.createNewFile(File.java:1008)
01-15 13:46:34.941 21875 22498 W System.err: 	at com.imagepicker.utils.MediaUtils.createNewFile(MediaUtils.java:59)
01-15 13:46:34.941 21875 22498 W System.err: 	at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:274)
01-15 13:46:34.941 21875 22498 W System.err: 	at java.lang.reflect.Method.invoke(Native Method)
01-15 13:46:34.941 21875 22498 W System.err: 	at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:371)
01-15 13:46:34.941 21875 22498 W System.err: 	at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:150)
01-15 13:46:34.941 21875 22498 W System.err: 	at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
01-15 13:46:34.941 21875 22498 W System.err: 	at android.os.Handler.handleCallback(Handler.java:873)
01-15 13:46:34.941 21875 22498 W System.err: 	at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 13:46:34.941 21875 22498 W System.err: 	at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:26)
01-15 13:46:34.941 21875 22498 W System.err: 	at android.os.Looper.loop(Looper.java:193)
01-15 13:46:34.942 21875 22498 W System.err: 	at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:225)
01-15 13:46:34.942 21875 22498 W System.err: 	at java.lang.Thread.run(Thread.java:764)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 12
  • Comments: 41 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I was able to fix it by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file

In my case, I found a reason of that buggy behavior. To capture an image from the camera the app needs both permissions to be granted: android.permission.CAMERA and android.permission.WRITE_EXTERNAL_STORAGE. react-native-image-picker asks only for a camera.

So, all you need is just to ask for permission forcibly using a standard react-native component:

` import {PermissionsAndroid} from ‘react-native’;

const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { title: ‘We need your permission’ }, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { console.log(‘You can use the camera’);

                    ImagePicker.showImagePicker(options, async (response) => {
                        if (response.didCancel) {
                            console.log('User cancelled image picker');
                        } else if (response.error) {
                            console.log('ImagePicker Error: ', response.error);
                            console.log(response);
                        } else if (response.customButton) {
                            console.log('User tapped custom button: ', response.customButton);
                        } else {
                            const source = { uri: response.uri };
                        }
                    });

                } else {
                    console.log('Camera permission denied');
                }`

still a problem on Android 10 / API 29. Tried all suggested solutions.

actually fixed in #1295

All I can say is that I’ve been unable to fix Android 10 issue either.

I can verify this has been resolved in the 2.3.1 release.

Yeah , receiving the same error on emulator as well as device in android.

@olegzecamp, you rock, my friend! Thank you!

It’s not a solution, but I fixed by installing v 1.1.0

Version 2.x.x is not longer supported, please upgrade to 3.x.x

Permission have changed check the docs.

I hope the issue is solved, create new issue with details I am happy to fix it.

Still getting this issue on my android 10 device. Tried to add android:requestLegacyExternalStorage=“true” - doesn’t help. Reproduce rate 100%, any RN version from 0.61.2 up to 0.63.3, any version of react-native-image-picker up to 3.0.0-vnext.4. App restarts every time on trying to take picture from camera, and first time on selecting from gallery. Neither handling permissions or not - it doesn’t matter. Also any older apps gets this camera issue too, even if they worked 6 months ago - for now they’re not.

Please migrate to using version 3.x.x https://github.com/react-native-image-picker/react-native-image-picker/releases

Check 3.x.x docs for installation and API changes. Post if you have any issue

I was able to fix it by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file

I actually logged in to thank you, thanks so much!