【问题标题】:Trying to open pdf file Android试图打开pdf文件Android
【发布时间】:2016-01-31 23:50:33
【问题描述】:

我遇到了这个错误:

E/AndroidRuntime: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.mobgen.androidinterviewtest/files/LaFerrari.pdf
E/AndroidRuntime:     at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
E/AndroidRuntime:     at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
E/AndroidRuntime:     at com.mobgen.interview.SingleCarActivity$1.onClick(SingleCarActivity.java:92)

是因为这行代码:

Uri uri = FileProvider.getUriForFile(SingleCarActivity.this, "be.myapplication", file);

我已经关注了我找到的资源: http://developer.android.com/reference/android/support/v4/content/FileProvider.html

Open file in cache directory with ACTION_VIEW

How to use support FileProvider for sharing content to other apps?

但是我仍然无法解决我遇到的错误。

下面你可以看到我的代码:

SingleCarActivity.java:

File file = new File(getFilesDir(), pdf); //pdf contains "LaFerrari.pdf"
Uri uri = FileProvider.getUriForFile(SingleCarActivity.this, "be.myapplication", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);

AndroidManifest.xml:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="be.myapplication"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

file_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_pdf" path="assets/"/>
</paths>

您可以在下面看到我的项目结构的屏幕截图: 链接:http://i.imgur.com/4dbhcKF.png

【问题讨论】:

  • assets/ 是你的开发机器上的一个目录。它不是设备上的目录。 FileProvider 不能提供资产,只能提供本地文件。您是否已将这些文件从资产复制到 getFilesDir() 下的 assets/ 子目录?
  • @CommonsWare 我已经用谷歌搜索过了。您的意思是类似于以下问题的答案吗?链接:stackoverflow.com/questions/8474821/…

标签: java android xml android-fileprovider


【解决方案1】:

FileProvider 不能直接提供资产,因为资产不是文件。您的主要选择是:

  1. 将资产复制到您为FileProvider 配置的位置中的文件。我在this sample app 中演示了这种FileProvider 配置,但the question from your comment 的答案只是将资产复制到文件的基础知识。

  2. 使用my StreamProvider,可以直接服务资产,无需复制。

【讨论】:

  • 我使用了您的示例应用程序中的代码。但现在我面临这个新错误:No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://be.myapplication/my_pdf/LaFerrari.pdf flg=0x1 } 这是因为这行代码:startActivity(i); 我已经用谷歌搜索过,我发现了这个问题:stackoverflow.com/questions/9157490/… 所以我向 SingleCarActivity 添加了一个意图过滤器:pastebin.com/9jd2FACK 但是我仍然得到错误。我已将代码放在 pastebin 链接上:pastebin.com/NUNGxvcQ
  • @superkytoz:“但现在我遇到了这个新错误”——您需要在您的设备上安装一个 PDF 查看应用程序,该应用程序支持 content://Uri 值。 Adobe Reader、Foxit 和 ezPDF Reader 都可以,其他人可能也可以。
  • pdf 为空白。如何从资产中打开 pdf。
  • @septemberboy7:我的回答中涵盖了这一点。如果您正在使用其中一种技术,但它对您不起作用,请提出一个单独的 Stack Overflow 问题,并在其中提供minimal reproducible example
猜你喜欢
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2023-04-02
  • 2019-10-10
  • 2014-06-08
  • 1970-01-01
相关资源
最近更新 更多