Android : FileProvider on custom external storage folder

budgw picture budgw · May 6, 2016 · Viewed 32.2k times · Source

I'm trying to set up a fileprovider for sharing file. My files are saved in a folder "AppName" in the external storage (same level as Android, Movies and Pictures folders).

Here is my file provider config :

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.mydomain.appname.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"/>
</provider>

and the file_paths.xml :

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

When i try to access my file with :

Uri fileUri = FileProvider.getUriForFile(activity, "com.mydomain.appname.fileprovider",
            new File("/storage/emulated/0/AppName/IMG_20160419_095211.jpg"));

It returns an error: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/AppName/IMG_20160419_095211.jpg at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)

It worked fine before when I was using built-in directory like Pictures or Movies, my file_paths.xml was define like this :

<external-path name="photos" path="Pictures" />
<external-path name="videos" path="Movies" />

But now I want to store my file in my own folder. Did I miss something with the FileProvider config ?

Answer

aolphn picture aolphn · Oct 16, 2018
<files-path name="name" path="path" />  

Represents files in the files/ subdirectory of your app's internal storage area. This subdirectory is the same as the value returned by Context.getFilesDir().

<external-path name="name" path="path" />

Represents files in the root of the external storage area. The root path of this subdirectory is the same as the value returned by Environment.getExternalStorageDirectory().

<external-files-path name="name" path="path" />

Represents files in the root of your app's external storage area. The root path of this subdirectory is the same as the value returned by Context#getExternalFilesDir(String) Context.getExternalFilesDir(null).

for more details please check Android's doc of FileProvider. some configuration like following picture,com.view.asim.enterprise is my package name. enter image description here