问题描述
我一直在尝试关于共享文件的 Android教程.我这样设置FileProvider
:
I have been trying to follow the Android tutorial on sharing files.I set up the FileProvider
like this:
在主清单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>
res/xml/filpaths.xml文件:
the res/xml/filpaths.xml file:
<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]);
}
使用文件的正确工作路径实例化requestFile.该文件的路径完全以getExternalFilesDir(Environment.DIRECTORY_PICTURES)
返回的内容开头.我只是不明白是什么引起了错误,因为一切似乎都合适.预先感谢.
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.
推荐答案
AFAIK,不会为您提供名为SpCars_album/
的目录.
AFAIK, that will not give you a directory named SpCars_album/
.
您提供的文件不是FileProvider
可以从已定义的根目录提供的文件.
The file you supplied is not one that can be served by the FileProvider
from your defined roots.
更新
我忘记了这与 FileProvider
上的文档错误有关. FileProvider
和<external-path>
不会不提供getExternalFilesDir()
中的文件,而不是Environment.getExternalStorageDirectory()
中的文件.我创建了一个StreamProvider
子类,该子类提供对getExternalFilesDir()
的支持.
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()
.
如果您使用我的StreamProvider
,将<external-path>
替换为<external-files-path name="myexternalimages" path="Pictures/SpCars_album/" />
,那么您的运气会更好.
If you use my StreamProvider
, replacing your <external-path>
with <external-files-path name="myexternalimages" path="Pictures/SpCars_album/" />
, you should have better luck.
这篇关于IllegalArgumentException:无法在FileProvider.getUriForFile上找到包含xxx的配置根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!