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

Created on 15 Jan 2020  ·  40Comments  ·  Source: react-native-image-picker/react-native-image-picker

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)

Most helpful comment

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

All 40 comments

I am having the same issue. Did you manage to fix it? The solutions in https://github.com/react-native-community/react-native-image-picker/issues/928#issuecomment-438526153 didn't help.

I am using React-native 0.61.5 and Image Picker v 2.0.0

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

I have this same error also. I have managed to do step on this issue, but didn't work. Im using image picker 2.2.0

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');
                }`

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

@olegzecamp I already assumed it had something to do with this. But one of the main points of the v2.0.0 release was to split the two permissions so you would not need to give file permissions when only using the camera (see the v2.0.0 release, which contains this commit).

So while this does work, I don't think it's the right fix, given that these permissions were split intentionally.

Just checked, this issue is still active in v2.3.0.

yeah, for some reason camera launch requires file write permission. @olegzecamp workaround will do for now

actually fixed in #1295

Anxiously waiting for this merge

I got uri when take a picture from camera but I cant show it. How to solve it?

I got uri when take a picture from camera but I cant show it. How to solve it?

@1511011 use the base64 response string

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

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

Is this compatible with Android 10? I have upgraded to 2.3.1 and still getting this error.

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

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

Facing the same issue here. Android 10

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

Emulator or physical device?

both.

ons. 29. apr. 2020, 16:18 skrev Rafael Cavalcante <[email protected]

:

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

Emulator or physical device?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/react-native-community/react-native-image-picker/issues/1269#issuecomment-621241028,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAUZK6A4BTG5UDEMTFVMLQTRPAZLLANCNFSM4KHDHDXA
.

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

I'm facing it as well on Android 9

Edit: #928 (comment) Fixed it :)

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

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!

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

Note, from : https://developer.android.com/training/data-storage/use-cases#opt-out-scoped-storage
"Caution: After you update your app to target Android 11 (API level 30), the system ignores the requestLegacyExternalStorage attribute when your app is running on Android 11 devices, so your app must be ready to support scoped storage and to migrate app data for users on those devices."

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

Thanks you

Just did a PR with the fixes, for the moment you can add in your package.json the following:

"react-native-image-picker": "https://github.com/iagormoraes/react-native-image-picker",

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

Thanks, it works for me!

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

I tried 2.3.1 but not fixed!

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

Magic! Thanks!

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

You, sir, are a legend! Thanks a lot!

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

magia :astonished:

Thanks.It works.

在 2020-10-20 21:22:08,"Afraz Hussain" notifications@github.com 写道:

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

You, sir, are a legend! Thanks a lot!


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

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'm not sure what's causing this error, but I have two situations here that I think need to be exposed

I have 2 projects, with different versions of react-native but with same version of react-native-image-picker

Project 1 (works on Android 10):

  • react-native: 0.61.5
  • react-native-image-picker: 2.3.1

Project 2 (does not work on Android 10):

  • react-native: 0.63.3
  • react-native-image-picker: 2.3.1

on Project 2 by adding the solution of @rishiankush it did work

So I'm guessing there was something changed in react-native itself that made image-picker not work on Android 10

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.

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

You save me, Thanks

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.

@hengsoheak android/app/src/main/AndroidManifest.xml

At the end of <application ... >
under android:theme="@style/AppTheme" for example

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

You solved my problem on a legacy application! Thank you!

Was this page helpful?
0 / 5 - 0 ratings