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

u2g*_*les 7 android android-sdcard android-fileprovider

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

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

 Uri videoUri = FileProvider.getUriForFile(this,
            getApplicationContext().getPackageName() + ".provider",
            new File(videoPath));
Run Code Online (Sandbox Code Playgroud)

这里videoPath具有以下值:

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

我的清单文件包含:

       <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>
Run Code Online (Sandbox Code Playgroud)

这里是provider_paths:

<external-path name="external_files" path="."/>
Run Code Online (Sandbox Code Playgroud)

如何修改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)
Run Code Online (Sandbox Code Playgroud)

其他配置信息:

compileSdkVersion 25

buildToolsVersion "23.0.3"

minSdkVersion 16

targetSdkVersion 25

support libraries version : 25.1.1   
Run Code Online (Sandbox Code Playgroud)

小智 30

我在我的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>
Run Code Online (Sandbox Code Playgroud)

  • 这必须是公认的答案,即使它不在官方文档中. (4认同)

Com*_*are 2

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

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