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 ?
<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.
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
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
<external-files-path
name="external_files"
path="." />
<!-- FOR SD CARD-->
<root-path
name="sdcard1"
path="." />
</paths>
I fixed it
<!--THIS IS EVIL-->
<root-path
name="sdcard1"
path="." />
I had the same thing. In my case I had to clean the build in Android Studio (Build > Clean Project) every time I modified the path of the fileprovider in 'file_paths.xml'.
In <external-path name="mypath" path="AppName" />
name="mypath" - mypath must be a specific piece from your image file name, say name="IMG" would work if all your image files have "IMG" in their name, judging by your sample code it is the case here.
path="AppName" is a folder name that you created in external for your images and it needs "/" at the end, i.e. path="AppName/"
so, <external-path name="IMG" path="AppName/" />
should do it and FileProvider should find and let other apps access your images when you request Uri with FileProvider.getUriForFile().
Hope it helps.
Replace your xml path with
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="extfiles" path="."/> </paths>
来源:https://stackoverflow.com/questions/37074872/android-fileprovider-on-custom-external-storage-folder