react-native-share: Android: FileProvider Issue: Failed to find configured root that contains ...

my problem is related to #202 . I am also using react-native-image-crop-picker which implements FileProvider in their own Manifest. Therefore I override their FileProvider Entry in my Manifest (note: I do not override the android:authorities entry, but the path ressource):

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
      <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          tools:replace="android:resource"
          android:resource="@xml/file_paths" />

file_paths.xml looks like this (note: I also added the path defined by react-native-image-crop-picker 's Manifest):

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="files" path="."/>
    <external-files-path name="external_files_path" path="." />
    <external-path name="external_files" path="."/>
</paths>

I also edited my MainApplication and added the following (note: BuildConfig.APPLICATION_ID instead of ${applicationId} as in this docs, saw this suggestion in one PR):

@Override
  public String getFileProviderAuthority() {
    return BuildConfig.APPLICATION_ID + ".provider";
  }

Now, trying really everything, I do not believe anymore that it is caused (only) by react-native-image-crop-picker as in #202 . I now get this error:

Failed to find configured root that contains /external_files/test.txt

My react-native use of the Share library looks this way:

content.url = "content://com.myapp.provider/external_files/test.txt";
Share.open(content).catch((err) => { err && console.log(err); });

and the file is created before at dir: RNFS.ExternalDirectoryPath + "/test.txt". I have also tried every other RNFS Directory (e.g. DocumentDirectoryPath). The file is really there, i can read it with RNFS.

What am I missing? Have now worked on this for days and really need to get around this problem.

Versions:

react-native-share: 1.0.26 react-native: 0.52.2 react-native-image-crop-picker: 0.19.1

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 25 (2 by maintainers)

Most upvoted comments

try this at your file_paths

<paths xmlns:android="http://schemas.android.com/apk/res/android"> <root-path name="root" path="." /> </paths>

try this at your file_paths

<paths xmlns:android="http://schemas.android.com/apk/res/android"> <root-path name="root" path="." /> </paths>

I LOVE YOU THANK YOU SO MUCH

I resolved as bellow, may be can help someone.

Step1: In the file app/src/main/AndroidManifest.xml,

  1. Use xmlns:tools Example in my app:
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" package="com.ulo" 
...

  1. add provider between the <application> and </application> tag
<provider  android:name="android.support.v4.content.FileProvider" android:authorities="com.ulo.provider" android:grantUriPermissions="true" android:exported="false">
                <meta-data 
tools:replace="android:resource"
android:name="android.support.FILE_PROVIDER_PATHS" 
android:resource="@xml/provider_paths" />
</provider>

Step2: Create file provider_paths.xml at app/src/main/res/xml/ Example in my app:

<?xml version="1.0" encoding="utf-8"?>
  <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-path name="external_files" path="."/>
      <external-path name="myexternalimages" path="Download/" />
  </paths>

Step3: Update MainApplication.java, implement ShareApplication

Example in my app:

import cl.json.ShareApplication;

public class MainApplication extends Application implements ShareApplication, ReactApplication {
...

@Override
  public String getFileProviderAuthority() {
        return "com.ulo.provider";
  }
...
}

Noted: com.ulo is my applicationId, change to your applicationId (you can check id in app/build.gradle)

Package: react-native / react-native-share / react-native-image-crop-picker

I ended up taking the URI and converting it to base64 , working in android, not tested in iOS

    if (Platform.OS === 'ios') {
        let arr = uri.split('/')
        const dirs = RNFetchBlob.fs.dirs
        filePath = `${dirs.DocumentDir}/${arr[arr.length - 1]}`
    } else {
        filePath = uri
    }

    RNFetchBlob.fs.readFile(filePath, 'base64')
        .then((data) => {
            // handle the data ..
            console.log(data)
            const IMAGE = 'data:image/png;base64,'+data
            let shareImageBase64 = {
                title: "React Native",
                message: "Hola mundo",
                url: IMAGE,
                subject: "Share Link" //  for email
            };
    
    
            Share.open(shareImageBase64);
        })

@vforvasile, besides what @tuananhluong posted, I added the following line to my filepaths.xml: <external-files-path name="external_files" path="." /> This seems to have fixed my issue

I have the same problem, my file is saved at: file:///data/user/0/com.applicationId/cache/image_manager_disk_cache/a551283a40d90aa951eb098148ed0a6423a78ec29f8df43a7fe2a79a7f85794d.0

and getting this error: Failed to find configured root that contains /data/data/com.applicationId/cache/image_manager_disk_cache/a551283a40d90aa951eb098148ed0a6423a78ec29f8df43a7fe2a79a7f85794d.0

I have already applied all possible path in provider path and still no luck <root-path name="root" path="." /> <files-path name="files" path="." /> <cache-path name="cached_files" path="." /> <external-path name="external_files" path="." /> <external-cache-path name="cached_files" path="." /> <external-files-path name="external_files" path="." />

Not fixed, not very fun.

try this at your file_paths

<paths xmlns:android="http://schemas.android.com/apk/res/android"> <root-path name="root" path="." /> </paths>

Doesn`t seem like a secure solution 😕 https://developer.android.com/topic/security/risks/file-providers#mitigations

@vforvasile which errors are you experiencing?