【问题标题】:Android Fileprovider: IllegalArgumentException: Failed to find configured root that containsAndroid Fileprovider: IllegalArgumentException: 未能找到配置的根目录包含
【发布时间】:2013-12-14 07:58:11
【问题描述】:

我有一个关于 android FileProvider 的问题。 我想保存一个 pdf 文档并使用默认程序打开它。 我不想将它保存在外部存储中。

在我成功将 pdf 保存到 FilesDirectory/export/temp.pdf 后,
我尝试使用 FileProvider.getUriForFile() 生成 URI。

File path = new File(getFilesDir(), "export");
File pdf = new File(path + File.separator + "temp.pdf");
pdf.getParentFile().mkdirs();

if (!pdf.exists())
    pdf.createNewFile();

Uri uri = FileProvider.getUriForFile(getApplicationContext(), "?", pdf);

问题:我必须传递什么作为第二个参数“权限” - 我的文件的位置、可以授予 URI-Permissions 的类或其他什么?无论我尝试过什么都会导致 IllegalArgumentException 或 NullPointerException。 我的文件提供者(XML):

<provider         
    android:name="android.support.v4.content.FileProvider"           
        android:authorities="com.example.myApp.myActivity"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data                 
             android:name="android.support.FILE_PROVIDER_PATHS"                           
             android:resource="@xml/file_path"/>                                       
</provider>

参考文件:

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

【问题讨论】:

  • 我也有同样的问题。你找到答案了吗?

标签: pdf authority android-fileprovider


【解决方案1】:

我明白了。有两个不同的问题

  1. 第一个问题由 CodeDiving 回答。 对于 getUriForFile 调用,我必须使用提供者声明中的授权。使用其他类导致 NullPointerException。

  2. 我尝试从 filesDirectory 获取文件,但在我的 file_path 中我只声明了缓存目录的路径。我将其更改为“文件路径”并且它有效。此错误导致 IllegalArgumentException。

【讨论】:

  • 确保它是files-path 而不是file-path。很容易错过!
  • files-path 去哪儿了?我的意思是你在哪里指定它。
  • @IgorGanapolsky 他将引用的 xml 文件 file_path 中的 &lt;cache-path 替换为 &lt;files-path
  • 如果您使用的是getCacheDir(),那么可以使用cache-path 而不是files-path。如果您将文件直接转储到 getCacheDir,则无需指定 path=
【解决方案2】:

根据您的 FileProvider 文件 (XML),第二个参数是 com.example.myApp.myActivity。那是

Uri uri = FileProvider.getUriForFile(getApplicationContext(),
                                     "com.example.myApp.myActivity", pdf);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多