如何打开存储在res / raw或assets文件夹中的pdf? [英] How to open a pdf stored either in res/raw or assets folder?

查看:175
本文介绍了如何打开存储在res / raw或assets文件夹中的pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在我的应用程序中显示一个pdf,并且该pdf必须与应用程序捆绑在一起。



这是一个很好的方法?



我已经阅读了,可以通过将pdf文件添加到res / raw文件夹中并从中读取,但是当我发现项目错误时可能会这样做把pdf文件放在那里。



所以我试图将pdf文件放在项目的资产文件夹中,没有发生错误。



这是我试图显示pdf的方式:

  File pdfFile = new File( RES /原料/ file.pdf); 
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path,application / pdf);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

任何想法或建议?



谢谢提前

解决方案

您可以从 raw / assets / 如果您的应用程序实际上实现了PDF阅读器。由于您希望将其显示在单独的应用程序(如Adobe Reader)中,我建议您执行以下操作:


  1. 存储PDF文件在资产/ 目录中。

  2. 当用户想要查看它时,将其复制到 public 。查看 openFileOutput getExternalFilesDir

  3. 启动就像你现在在做,除了使用 对于意图的数据,新创建的文件中的getAbsolutePath()

请注意用户可能没有PDF阅读应用程序。在这种情况下,抓住 ActivityNotFoundException ,并显示相应的消息。


I'm going to show a pdf in my application, and the pdf has to be bundled with the application.

What is a good way to do this?

I have read that it might be possible to do this by adding the pdf file to a res/raw folder and read it from there, but i get project errors when i put the pdf file there.

So i tried to put the pdf file in the asset folder of the project, and it gave no errors.

This is how i've tried to show the pdf:

File pdfFile = new File("res/raw/file.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Any ideas or suggestions?

Thanks in advance

解决方案

You would be able to show it from raw/ or assets/ if your application actually implemented a PDF reader. Since you want it to be displayed in a separate application (such as Adobe Reader), I suggest doing the following:

  1. Store the PDF file in the assets/ directory.
  2. When the user wants to view it, copy it somewhere public. Look into openFileOutput or getExternalFilesDir.
  3. Launch the Intent just like you are doing now, except use getAbsolutePath() on the newly created file for the intent's data.

Be aware that a user might not have a PDF reading application. In this case, it is useful to catch the ActivityNotFoundException and show an appropriate message.

这篇关于如何打开存储在res / raw或assets文件夹中的pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆