简体   繁体   English

尝试打开pdf文件Android

[英]Trying to open pdf file Android

I'm facing this error: 我遇到这个错误:

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)

It's because of this line of code: 因为这行代码:

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

I have followed the resources that I had found: http://developer.android.com/reference/android/support/v4/content/FileProvider.html 我遵循了我发现的资源: http : //developer.android.com/reference/android/support/v4/content/FileProvider.html

Open file in cache directory with ACTION_VIEW 使用ACTION_VIEW在缓存目录中打开文件

How to use support FileProvider for sharing content to other apps? 如何使用支持FileProvider来与其他应用程序共享内容?

However I still couldn't manage to fix the error that I'm having. 但是,我仍然无法解决所出现的错误。

Below you can see my code: 在下面您可以看到我的代码:

SingleCarActivity.java: 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: 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: file_paths.xml:

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

Below you can see a screenshot of how my project structure looks like: Link: http://i.imgur.com/4dbhcKF.png 在下面,您可以看到我的项目结构的屏幕截图:链接: http : //i.imgur.com/4dbhcKF.png

FileProvider cannot serve assets directly, as assets are not files. FileProvider无法直接服务资产,因为资产不是文件。 Your primary choices are: 您的主要选择是:

  1. Copy the asset to a file, in a location that you have configured for FileProvider . 将资产复制到文件中您已为FileProvider配置的FileProvider I demonstrate that sort of FileProvider configuration in this sample app , but the answers to the question from your comment do the basics of copying the asset to a file. 我在此示例应用程序中演示了这种FileProvider配置,但是您评论中对该问题的回答做了将资产复制到文件的基础。

  2. Use my StreamProvider , which can serve assets directly, without having to make a copy. 使用我的StreamProvider ,它可以直接提供资产,而无需进行复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM