React-native-image-picker: Couldn't get file path for photo on ANDROID 10 EMU

Created on 23 Apr 2020  路  28Comments  路  Source: react-native-image-picker/react-native-image-picker

Bug

Camera doesn't open after granting permission and selecting Take Photo from popup.

Previously I was using version 1.1.0 on Android 9 without issue. After upgrading to Android 10 I started getting this error. Issue 1269 is confirming the latest build v2.3.1 fixes this issue but after upgrading to 2.3.1 I am still getting the same error. This is on an Android 10 emulator.

react-native info output:

System:
OS: Windows 10 10.0.17134
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Memory: 2.49 GB / 15.80 GB
Binaries:
Node: 10.16.3 - C:Program Filesnodejsnode.EXE
Yarn: 1.17.3 - C:Program Files (x86)Yarnbinyarn.CMD
npm: 6.9.0 - C:Program Filesnodejsnpm.CMD
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.5 => 0.61.

Library version: ^2.3.1

Buildscript:

ext {
        buildToolsVersion =  "29.0.2"
        minSdkVersion = 16
        supportLibVersion = "29.0.0"
        compileSdkVersion = 29
        targetSdkVersion = 29
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.4.2")

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

Usage:

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

        } 

   //ImagePicker Error:  Couldn't get file path for photo

The workaround in 1269 asking for permission did not work either. I still log the same error.
```
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');
    }

```
Did I miss a step here?

Most helpful comment

I was able to apply a short term fix to get this going by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file.

I believe this is due to Android 10 scoped storage privacy changes and will require updating the library and how it reads/writes from the filesystem. My android dev experience is very limited but will submit a PR if I am able to figure it out.

All 28 comments

Have you tried giving permission of external storage as well explicitly ?? @MOwais
Is that not what the workaround is doing?

Having the same issue in Android 10/ API 29..

Having the same issue in Android 10!

I was able to apply a short term fix to get this going by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file.

I believe this is due to Android 10 scoped storage privacy changes and will require updating the library and how it reads/writes from the filesystem. My android dev experience is very limited but will submit a PR if I am able to figure it out.

Having the same issue in Android 10!

+1, help!

I was able to apply a short term fix to get this going by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file.

I believe this is due to Android 10 scoped storage privacy changes and will require updating the library and how it reads/writes from the filesystem. My android dev experience is very limited but will submit a PR if I am able to figure it out.

Thanks, it works for me.

I was able to apply a short term fix to get this going by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file.

I believe this is due to Android 10 scoped storage privacy changes and will require updating the library and how it reads/writes from the filesystem. My android dev experience is very limited but will submit a PR if I am able to figure it out.

This didn't work for me

I was able to apply a short term fix to get this going by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file.

I believe this is due to Android 10 scoped storage privacy changes and will require updating the library and how it reads/writes from the filesystem. My android dev experience is very limited but will submit a PR if I am able to figure it out.

It worked for me as well! Thanks a lot!

I was able to apply a short term fix to get this going by adding android:requestLegacyExternalStorage="true" to application tag in AndroidManifest.xml file.

I believe this is due to Android 10 scoped storage privacy changes and will require updating the library and how it reads/writes from the filesystem. My android dev experience is very limited but will submit a PR if I am able to figure it out.

This is only a short term fix, what's the long term approach?

requestLegacyExternalStorage will be ignored as soon as you choose targetSdk 30. See: https://developer.android.com/preview/privacy/storage

To give developers additional time for testing, apps that target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows apps to temporarily opt out of the changes associated with scoped storage, such as granting access to different directories and different types of media files. After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag.

So as @paulmasters asked, we need a long term fix for this.

We also have this issue on Android 10!
The requestLegacyExternalStorage fix does solve the problem, but definitely is just a temporary fix, but with Android 11 beta coming out this month, we probably need a more definite fix!
image

Has anyone started investigating what the fix would entail or started on it!

any news for this issue ?

@lotusshinoaki
maybe create a pull request please?

@mhashim6
There was a problem with fixing https://github.com/lotusshinoaki/react-native-image-picker/commit/760299874f2a3075573f66ad251ab273e283fbd1
Unable to get metadata (such as MediaStore.MediaColumns.WIDTH) from URL using ContentResolver.
So I don't think this is generally a good (and well worth creating PR) solution.

A possible workaround (if you don't need your pics to be public) is to use the privateDirectory flag:

storageOptions={{ privateDirectory: true  }}

Check if there are other modules in your project that use the same provider. If so, please change this module AndroidManifest.xml :<provider android:name="com.imagepicker.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>
to
` android:name="com.imagepicker.FileProvider"
android:authorities="${applicationId}.PickProvider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/pick_provider_paths" />

`

RealPathUtil.java:
provider
to
PickProvider

Check if there are other modules in your project that use the same provider. If so, please change this module AndroidManifest.xml :<provider android:name="com.imagepicker.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>

to
<provider android:name="com.imagepicker.FileProvider" android:authorities="${applicationId}.PickProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/pick_provider_paths" /> </provider>
RealPathUtil.java:
provider
to
PickProvider

I have this module called "rn-update-apk" which requires to have a like this :
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <!-- you might need the tools:replace thing to workaround rn-fetch-blob or other definitions of provider --> <!-- just make sure if you "replace" here that you include all the paths you are replacing *plus* the cache path we use --> <meta-data tools:replace="android:resource" android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider>

I'm not able to change it. Do you have any idea how to fix this problem?

Check if there are other modules in your project that use the same provider. If so, please change this module AndroidManifest.xml :<provider android:name="com.imagepicker.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>

to
<provider android:name="com.imagepicker.FileProvider" android:authorities="${applicationId}.PickProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/pick_provider_paths" /> </provider>

RealPathUtil.java:
provider
to
PickProvider

I can confirm that your solution works - I have added a PR addressing that.

Check if there are other modules in your project that use the same provider. If so, please change this module AndroidManifest.xml :<provider android:name="com.imagepicker.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>

to
<provider android:name="com.imagepicker.FileProvider" android:authorities="${applicationId}.PickProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/pick_provider_paths" /> </provider>
RealPathUtil.java:
provider
to
PickProvider

Not Working!!!!!!!!!

A possible workaround (if you don't need your pics to be public) is to use the privateDirectory flag:

storageOptions={{ privateDirectory: true  }}

works in Android 10 using this flag but image file not saved in storage and choose file from gallery still not working

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",

If possible can you migrate to using version 3.0.0-vnext.1 https://github.com/react-native-image-picker/react-native-image-picker/releases/tag/v3.0.0-vnext.1

Check if your issue is solved.
Check 3.x.x docs for installation and API changes.

Hi,
I have this problem with [email protected], as well as this one #1475 , both Android. (didn't try iOS yet).
My RN version is 0.63.3 and it was working well before i updated it.
I tried to update react-native-image-picker to 3.0.0 but i have the same errors.

Hello Guys, I think this is not for everyone, but in my build.gradle file it was configured to use API 29.0.2
I just switched to API 28.0.3 - Sure with the SDK and NDK properly installed and working ...
After that I ran the Build and it worked fine on my android 10 device

Hello Guys, I think this is not for everyone, but in my build.gradle file it was configured to use API 29.0.2
I just switched to API 28.0.3 - Sure with the SDK and NDK properly installed and working ...
After that I ran the Build and it worked fine on my android 10 device

@NeuroBanco I have the same configuration. Does it affect if the compile and targetSdkVersion is 29? or should I move back to 28?

@jandrell-formoso I'm back to 28!

Google Play Store now requires to target API 29 as you can see here https://developer.android.com/distribute/best-practices/develop/target-sdk

Was this page helpful?
0 / 5 - 0 ratings

Related issues

woodpav picture woodpav  路  26Comments

DIVIBEAR picture DIVIBEAR  路  31Comments

himanshusharma9400 picture himanshusharma9400  路  39Comments

zscaiosi picture zscaiosi  路  81Comments

ferminmoli picture ferminmoli  路  26Comments