问题描述
我要显示PDF在我的应用程序,以及PDF格式必须与应用程序捆绑在一起。
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?
我已阅读,有可能通过添加PDF文件到RES /原始文件夹要做到这一点,并从那里读它,但我得到的项目错误,当我把pdf文件存在。
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.
所以,我试图把PDF文件在该项目的资源文件夹,它给了没有错误。
So i tried to put the pdf file in the asset folder of the project, and it gave no errors.
这就是我一直在努力,显示PDF:
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?
在此先感谢
推荐答案
您将能够从显示它生/
或资产/
如果您的应用程序实际上实现了一个PDF阅读器。既然你想让它显示在一个单独的应用程序(如Adobe Reader的),我建议做如下:
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:
- 保存在
资产/
目录中的PDF文件。 - 当用户想查看某个地方将它复制公开。看看<$c$c>openFileOutput$c$c>或<$c$c>getExternalFilesDir$c$c>.
- 启动
意图
就像你现在正在做,除非使用<$c$c>getAbsolutePath()$c$c>上为意图的数据新创建的文件。
- 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.
请注意,用户可能没有一个PDF阅读应用程序。在这种情况下,捕捉是有用的<$c$c>ActivityNotFoundException$c$c>并显示相应的消息。
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.
这篇关于如何打开存储无论是在资源/原材料或资产文件夹中的PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!