Android 文件提供程序:IllegalArgumentException:找不到包含 /external/video/media/678 的已配置根目录

Android File Provider: IllegalArgumentException: Failed to find configured root that contains /external/video/media/678

我正尝试从图库中获取图像,但我的文件提供程序出了点问题。

开启

Uri contentUri = getUriForFile(getContext(), "com.pkg.name.fileprovider", result);

我得到这个异常:

      11-16 16:57:54.923 24662-24662/com.pkg.name E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.pkg.name, PID: 24662
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=20, result=-1, data=Intent { (has extras) }} to activity {com.pkg.name/com.pkg.name.activities.MainNavigationActivity}: java.lang.IllegalArgumentException: Failed to find configured root that contains /external/video/media/678
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3845)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3888)
        at android.app.ActivityThread.access00(ActivityThread.java:178)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5643)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
     Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /external/video/media/678
        at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
        at com.pkg.name.fragments.ChatFragment.sendVideo(ChatFragment.java:620)
        at com.pkg.name.fragments.ChatFragment.access00(ChatFragment.java:80)
        at com.pkg.name.fragments.ChatFragment.onResult(ChatFragment.java:603)
        at com.pkg.name.fragments.ChatFragment.onResult(ChatFragment.java:600)
        at com.pkg.name.controllers.AttachmentPanelController.commitText(AttachmentPanelController.java:214)
        at com.pkg.name.fragments.ChatFragment.commitText(ChatFragment.java:635)
        at com.pkg.name.activities.MainNavigationActivity.onActivityResult(MainNavigationActivity.java:417)
        at android.app.Activity.dispatchActivityResult(Activity.java:6301)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3841)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3888) 
        at android.app.ActivityThread.access00(ActivityThread.java:178) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:194) 
        at android.app.ActivityThread.main(ActivityThread.java:5643) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

这是我的 @xml/file_paths

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="photos" path="Pictures/" />
    <files-path name="videos" path="Videos/" />
    <files-path name="temp" path="temp/" />
    <external-path name="root" path="/" />
</paths>

我认为 root 命名路径无法访问:

/external/video/media/

因为在媒体保留存储部分

It comes from new File(tempVideo.getPath()) where Uri tempVideo comes from a Intent.ACTION_PICK intent

A Uri 不是文件。仅当 Uri 的方案为 file 时,在 Uri 上调用 getPath() 才有效。在你的情况下,方案是 content,在这种情况下 getPath() 是没有意义的。而且,由于不要求用户选择实际上是文件的内容,因此请按原样使用 Uri。特别是,由于您似乎正在积极尝试获得 content Uri,因此认识到 您已经拥有一个 ,并尝试不加修改地直接使用它。