java.lang.IllegalArgumentException: 找不到包含 /file:/ 的已配置根目录

java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/

我正在尝试打开 DownloadManager 下载的文件。我用 DownloadManager.ACTION_DOWNLOAD_COMPLETE.

注册了一个 BroadcastReceiver

这是我的接收器的 onReceive 方法-

try {
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);

    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(downloadId);

    Cursor c = dm != null ? dm.query(query) : null;
    Log.d("Cursor", String.valueOf(c));
    String uri = null;
    if (c != null && c.moveToFirst()) {
        uri = c.getString(c.getColumnIndex("local_uri"));
    }
    Log.d("URI", uri);

    Intent open = new Intent(Intent.ACTION_VIEW);
    open.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    //Get mime type
    String mimeType = "*/*";
    String extension = null;
    extension = MimeTypeMap.getFileExtensionFromUrl(uri);
    if (extension != null) {
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }

    //Set data and start activity  
    open.setDataAndType(FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", new File(Uri.parse(uri).getPath())), mimeType);    //It shows error at this line 
    startActivity(install);
} catch (Exception e) {
    e.printStackTrace();
}  

这是我的 AndroidManifest.xml 文件中的 provider 标签 -

<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.xml 文件 -

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path
        name="external_files"
        path="/"/>
</paths>  

但是当我 运行 我的应用程序时,它在尝试打开文件时显示此错误:
java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/...(uri)

完整的堆栈跟踪如下所示:

01-14 20:49:49.069 21296-21296/in.edu.jaduniv.classroom W/System.err: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Classroom/juit1620/Syllabus/1405929814-1-14.pdf
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at in.edu.jaduniv.classroom.activity.Syllabus.onReceive(Syllabus.java:149)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1122)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at android.os.Looper.loop(Looper.java:154)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6123)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-14 20:49:49.070 21296-21296/in.edu.jaduniv.classroom W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
01-14 20:50:21.336 21296-21358/in.edu.jaduniv.classroom W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
01-14 20:54:02.582 21296-23083/in.edu.jaduniv.classroom D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=338004, firebase_screen_class(_sc)=Syllabus, firebase_screen_id(_si)=-6138131816728422160}]
01-14 20:54:02.611 21296-23083/in.edu.jaduniv.classroom D/FA: Connected to remote service

我该怎么办?
谢谢!

在您的 FileProvider.getUriForFile() 调用中将 new File(uri) 替换为 new File(Uri.parse(uri).getPath())。您从 local_uri 获得的值似乎是 Uri,具有 file 方案。

此外,根据 Uri 的其余部分,您可能需要将 external-files-path 替换为 external-path