[Android] FileProvider - IllegalArgumentException: Failed to find configured root

2018. 9. 20. 12:19Programing/Debugging

https://developer.android.com/reference/android/support/v4/content/FileProvider#SpecifyFiles


[Solution]

Step 1. Modify Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="com.victor.filepicker">

<application
    android:allowBackup="true"
    android:supportsRtl="true">
    ...
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.victor.filepicker.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"
            tools:replace="android:resource" />
    </provider>
</application>


 

Step 2. Add "file_paths" as res/xml/file_paths.xml

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


 

Step 3. Add code as below. 

File f = new File(file.getPath());
uri = FileProvider.getUriForFile(mContext, "com.victor.filepicker.provider", f);