android – FileProvider无法使用找不到包含的已配置根目录

我正在尝试使用FileProvider方式在Android上轻松下载和共享PDF文件以供其他PDF阅读器应用程序(DropBox,Drive PDF Reader或Adobe Reader)阅读.

但是,我一直得到以下异常:

Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf

我首先将URL发送到Android DownloadManager以执行下载,然后使用broadcastReceiver接收结果

我的实施如下:

file_paths.xml

<paths>

    <external-files-path
        name="my_docs"
        path="docs" />

</paths>

AndroidManifest.xml中

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">

    <Meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />

</provider>

从MainActivity:

public void downloadFile(String urlString) {
    Uri uri = Uri.parse(urlString);

    String filename = "my_file_name.pdf";

    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setAllowednetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);

    request.setTitle("App is Downloading a File");
    request.setDescription("Downloading PDF File For App");

    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalFilesDir(mContext, "docs", filename);

    request.allowScanningByMediaScanner();

    mDownloadManager.enqueue(request);
}

最后来自我的自定义broadcastReceiver:

uriString是使用downloadManager.query(myQuery)通常的方式从cursor.getString(uriIndex)获取的.

private void startDocumentReaderUsingFileProvider(Context context, String uriString) {
    Uri parsedUri = Uri.parse(uriString);

    File file = new File(parsedUri.getPath());
    String packageName = context.getApplicationContext().getPackageName();

    // Exception thrown from the following line.
    Uri uri = FileProvider.getUriForFile(context, packageName + ".fileprovider", file);

    startActivity(context, uri);
}

private void startActivity(Context context, Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, "application/pdf");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    Intent chooser = Intent.createChooser(intent, "display PDF Reader");

    context.startActivity(chooser);
}

异常堆栈跟踪:

FATAL EXCEPTION: main
    Process: com.my.app, PID: 8972
    java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.my.app bqHint=4 (has extras) } in com.my.app.FilebroadcastReceiver@82a2a7
        at android.app.LoadedApk$Receiverdispatcher$Args.run(LoadedApk.java:1003)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7229)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
        at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
        at com.my.app.FilebroadcastReceiver.startDocumentReaderUsingFileProvider(FilebroadcastReceiver.java:85)
        at com.my.app.FilebroadcastReceiver.startDocumentReader(FilebroadcastReceiver.java:69)
        at com.my.app.FilebroadcastReceiver.onReceive(FilebroadcastReceiver.java:43)
        at android.app.LoadedApk$Receiverdispatcher$Args.run(LoadedApk.java:993)
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7229) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
06-07 12:56:50.981 8972-9463/com.my.app E/com.worklight.common.Logger$UncaughtExceptionHandler: Logger$UncaughtExceptionHandler.uncaughtException in Logger.java:442 :: Uncaught Exception
    java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.my.app bqHint=4 (has extras) } in com.my.app.FilebroadcastReceiver@82a2a7
        at android.app.LoadedApk$Receiverdispatcher$Args.run(LoadedApk.java:1003)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7229)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
        at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
        at com.my.app.FilebroadcastReceiver.startDocumentReaderUsingFileProvider(FilebroadcastReceiver.java:85)
        at com.my.app.FilebroadcastReceiver.startDocumentReader(FilebroadcastReceiver.java:69)
        at com.my.app.FilebroadcastReceiver.onReceive(FilebroadcastReceiver.java:43)
        at android.app.LoadedApk$Receiverdispatcher$Args.run(LoadedApk.java:993)
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7229) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

解决方法:

<paths>

    <external-files-path
        name="my_docs"
        path="docs" />

</paths>

将路径指定为docs时,不包含子目录.您得到异常,因为它不期望共享子目录.要包含它们/最后添加/如下:

<paths>

    <external-files-path name="my_docs" path="docs/" />

</paths>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...