Android IllegalArgumentException:无法在 FileProvider.getUriForFile 上找到包含 xxx 的配置根目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20455035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalArgumentException: Failed to find configuration root that contains xxx on FileProvider.getUriForFile
提问by Hummus
I have been trying to follow the Android tutorialon sharing files.
I set up the FileProvider
like this:
我一直在尝试遵循有关共享文件的Android 教程。我是FileProvider
这样设置的:
On the main manifest xml:
在主清单 xml 上:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.mysecondapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
the res/xml/filpaths.xml file:
res/xml/filpaths.xml 文件:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="myexternalimages" path="SpCars_album/" />
</paths>
And in my code I am trying the following:
在我的代码中,我正在尝试以下操作:
File requestFile = new File(mImageFilenames[position]);
try {
fileUri = FileProvider.getUriForFile(
SeventhActivity.this,
"com.example.mysecondapp.fileprovider",
requestFile);
} catch (IllegalArgumentException e) {
Log.e("File Selector",
"The selected file can't be shared: " +
mImageFilenames[position]);
}
The requestFile is instantiated with a proper working path for a file.
The path of that file begins exactly with what getExternalFilesDir(Environment.DIRECTORY_PICTURES)
returns.
I just cant understand what raises the error because everything seems to fit.
Thanks in advance.
requestFile 使用文件的正确工作路径进行实例化。该文件的路径与getExternalFilesDir(Environment.DIRECTORY_PICTURES)
返回的内容完全相同。我只是不明白是什么引发了错误,因为一切似乎都合适。提前致谢。
回答by CommonsWare
The path of that file begins exactly with what getExternalFilesDir(Environment.DIRECTORY_PICTURES) returns.
该文件的路径以 getExternalFilesDir(Environment.DIRECTORY_PICTURES) 返回的内容开头。
AFAIK, that will not give you a directory named SpCars_album/
.
AFAIK,那不会给你一个名为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.
您提供的文件不是FileProvider
您定义的根可以提供的文件。
UPDATE
更新
I forgot that this is tied to a documentation bug on FileProvider
. FileProvider
and <external-path>
does NOTserve files from getExternalFilesDir()
, but instead from Environment.getExternalStorageDirectory()
. I created a StreamProvider
subclass that offers support for getExternalFilesDir()
.
我忘了,这是绑在一个文档错误FileProvider
。FileProvider
并<external-path>
没有不为来自文件getExternalFilesDir()
,而是从Environment.getExternalStorageDirectory()
。我创建了一个StreamProvider
支持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.
如果您使用 my StreamProvider
,将您的 替换<external-path>
为<external-files-path name="myexternalimages" path="Pictures/SpCars_album/" />
,您应该有更好的运气。
回答by eAi
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.
由于 getExternalStorageDirectory 返回“/storage/emulated/0/”,因此共享 /Android(下一级)将允许访问您应用中的任何文件。
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!
这感觉不太对劲,但似乎确实有效!
回答by TA2018
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.
implementation 'com.android.support:support-v4:28.0.0'
在我的情况下,添加到 app gradlle 依赖项是有效的。请相应地更改 SDK 平台版本(在我的情况下为 28.0.0)。
回答by u6293931
if you dont try catch exception as IllegalArgumentException for FileProvider.getUriFoFile(), when you run your code ,you also will have this exception.
如果您不尝试将异常捕获为 FileProvider.getUriFoFile() 的 IllegalArgumentException,则在运行代码时,您也会遇到此异常。
try {
Uri uriForFile = FileProvider.getUriForFile(this,"com.fengfutong.bluetoothc2s.fileprovider", f);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}