如何打开存储在res / raw或assets文件夹中的pdf? [英] How to open a pdf stored either in res/raw or assets folder?
问题描述
这是一个很好的方法?
我已经阅读了,可以通过将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)中,我建议您执行以下操作:
- 存储PDF文件在
资产/
目录中。 - 当用户想要查看它时,将其复制到 public 。查看
openFileOutput
或getExternalFilesDir
。 - 启动
就像你现在在做,除了使用
对于意图的数据,新创建的文件中的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:
- Store the PDF file in the
assets/
directory. - When the user wants to view it, copy it somewhere public. Look into
openFileOutput
orgetExternalFilesDir
. - Launch the
Intent
just like you are doing now, except usegetAbsolutePath()
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屋!