Android : FileProvider on custom external storage folder

我的梦境 提交于 2019-11-30 05:11:35
<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!