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

Android - FileProvider getUriForFile when the file is on an external SD

目前,当文件位于外部 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  

我的清单文件包含:

       <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   

您的提供商路径类型错误。您的 videoPath 显示了您应用程序外部存储的路径,但您的提供商路径使用的是 external-path,它链接到设备根外部存储。 (/storage/emulated/0)

将您的提供商路径更改为 <external-files-path>...</external-files-path>

How can I modify the FileProvider configuration to solve this problem?

你不能。 FileProvider 不支持移动存储。

我在 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>