IllegalArgumentException: Failed to find configuration root that contains xxx on FileProvider.getUriForFile

落爺英雄遲暮 提交于 2019-12-03 11:16:42

The path of that file begins exactly with what getExternalFilesDir(Environment.DIRECTORY_PICTURES) returns.

AFAIK, that will not give you a directory named SpCars_album/.

I just cant understand what raises the error because everything seems to fit.

The file you supplied is not one that can be served by the FileProvider from your defined roots.


UPDATE

I forgot that this is tied to a documentation bug on FileProvider. FileProvider and <external-path> does NOT serve files from getExternalFilesDir(), but instead from Environment.getExternalStorageDirectory(). I created a StreamProvider subclass that offers support for getExternalFilesDir().

If you use my StreamProvider, replacing your <external-path> with <external-files-path name="myexternalimages" path="Pictures/SpCars_album/" />, you should have better luck.

This is an old question now, but I've just had a similar issue.

A lazy solution I found is to just use:

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

Because getExternalStorageDirectory returns "/storage/emulated/0/", sharing /Android (the next level down) will allow any files that are in your app to be accessed.

Alternatively, if you want to be more precise, you can do this:

<external-path name="myexternalimages" path="Android/data/YOUR_BUNDLE_ID/files/SpCars_album" />

This doesn't feel quite right, but it does seem to work!

Adding implementation 'com.android.support:support-v4:28.0.0' into app gradlle dependencies worked in my case. Please change SDK platform version (28.0.0 in my case) accordingly.

if you dont try catch exception as IllegalArgumentException for FileProvider.getUriFoFile(), when you run your code ,you also will have this exception.

try {
     Uri uriForFile = FileProvider.getUriForFile(this,"com.fengfutong.bluetoothc2s.fileprovider", f);
} catch (IllegalArgumentException e) {
     e.printStackTrace();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!