Android - 当文件位于外部 SD 上时,FileProvider getUriForFile

标签 android android-sdcard android-fileprovider

目前,当文件位于外部 SD 上时,FileProvider getUriForFile 方法会生成 IllegalArgumentException

当文件在设备内存中(在/storage/emulated/0 下)时,它工作正常。

 Uri videoUri = FileProvider.getUriForFile(this,
            getApplicationContext().getPackageName() + ".provider",
            new File(videoPath));

此处 videoPath 具有以下值:

videoPath =  /storage/extSdCard/Android/data/com.podcastcutter.debug/files/episodeMp3/TEDTalks (video)/Why you should love statistics - Alan Smith.mp4  

我的 list 文件包含:

       <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

这里是 provider_paths :

<external-path name="external_files" path="."/>

如何修改FileProvider配置来解决这个问题?

提前致谢。

异常产生:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/extSdCard/Android/data/com.podcastcutter.debug/files/episodeMp3/TEDTalks (video)/Why you should love statistics - Alan Smith.mp4 
android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)                  
android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)

其他配置信息:

compileSdkVersion 25

buildToolsVersion "23.0.3"

minSdkVersion 16

targetSdkVersion 25

support libraries version : 25.1.1   

最佳答案

我在我的 provider.xml 中添加了这一行并且可以很好地从 SD 卡获取文件 URI:

<root-path name="external_files" path="/storage/" />

完整的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
    <root-path
        name="external_files"
        path="/storage/" />
</paths>

关于Android - 当文件位于外部 SD 上时,FileProvider getUriForFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42027914/

相关文章:

android - 如何为 RelativeLayout 边框添加背景颜色?

Android 文件夹的读/写权限

android - 无法将文件写入 Android kitkat 上的 SD 卡

在 android Kitkat 中找不到 android.support.v4.content.FileProvider 类

android - FileProvider.getUriForFile vs Uri.fromFile - 除了权限之外还有其他区别吗

android - 我们如何才能自动只在较新的 Android 版本上运行测试?

android - BaseLoaderCallback的Finish()方法不存在?

java - Tesseract 无法将数据复制到 SD?

c# - 如何在Unity中从Uri.fromfile转换为FileProvider.getUriForFile?

android - 带虚线的 Google Static Maps API 路径