Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : FileProvider on custom external storage folder

Tags:

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 ?

like image 935
budgw Avatar asked May 06 '16 14:05

budgw


2 Answers

<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

like image 183
aolphn Avatar answered Sep 16 '22 22:09

aolphn


First, I know this is an old post but it's the closest question posted that was similar to my problem so I'll post my solution.

The reason for that error is because the path you're supplying in the provider file is either

  • a) Spelled incorrectly and doesn't exist in the external-path
  • b) Using the /storage/emulated/0 absolute path

It returns Failed to find configured root that contains ... because it can't find that folder. So make sure you write only the directory you want to share, and ensure it's spelled correctly. Remember that when you declare external-path it is the equivelant of calling Enviornment.getExternalStorageDirectory() Since you write the name of the directory when you create your file, you don't need to provide a path in your provider file as all it does is mask whatever value is in the path with the name.

So your provider path would be: <external-path name="my_files" />

and your code would be:

File file = new File(new File(Environment.getExternalStorageDirectory(), "myfolder"), "file.ext"); Uri uri = FileProvider.getUriForFile(context, fileProvider, file); 

Your uri path would then yield the following

content://fileprovider/my_files/myfolder/file.ext

If you had supplied a path in your provider file then your uri path would look like this:

content://fileprovider/my_files/file.ext

like image 29
Pztar Avatar answered Sep 20 '22 22:09

Pztar