Android应用程序中assets文件夹中的pdf文件

发布于 2024-10-19 04:21:35 字数 1553 浏览 1 评论 0原文

我通过将 PDF 文件放置在 Android 应用程序的 assets 文件夹中来传送 PDF 文件。我可以使用 AssetManager 读取检索文件名。我需要将它们显示在屏幕上,我能够做到这一点。但我需要使用 pdfviewer 打开它们。

要在每个项目上编写 onclick() 意图,我需要传递 PDF 文件的 URI。使用 AssetManager 我无法获取文件的 URI。我有更好的方法来处理这整件事吗?

即使我们能够检索 URI,PDF 查看器是否能够读取 asset 文件夹中的 PDF 文件?

更新:
根据下面的建议,我编写了以下代码

  try {

                Uri path = Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample")  ;
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                }
                catch (ActivityNotFoundException e){
                    Toast.makeText(FirstTab.this, "NO Viewer", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

,它抛出 ActivityNotFoundException 并显示以下异常消息:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.csg.android.myproject/assets/pdf/csgsample typ=application/pdf flg=0x4000000 }

我使用 PackageManager 查询设备,发现安装了 Adob​​e reader。但我不确定它为什么会抛出这个错误。我还尝试将其放在 assets 文件夹中,然后相应地更改 URI 并运行程序,但最终还是出现相同的错误。

I am shipping PDF files by placing them in the assets folder of my android application. I am able to read the retieve the filenames using AssetManager. I need to display them on screen and I am able to do that. But I need to open them using a pdfviewer.

To write an intent on onclick() on each item, I need to pass the URI of the PDF file. And using the AssetManager I cannot get the URI of the file. Is there a better way I can handle this whole thing?

Even if we were able to retrieve the URI, can the PDF viewer be able to read the PDF file in the assets folder?

UPDATE:
Based on the suggestion below , I wrote the following code

  try {

                Uri path = Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample")  ;
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                }
                catch (ActivityNotFoundException e){
                    Toast.makeText(FirstTab.this, "NO Viewer", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

It throws an ActivityNotFoundException with the following exception message:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.csg.android.myproject/assets/pdf/csgsample typ=application/pdf flg=0x4000000 }

I queried the device using PackageManager and I found the Adobe reader installed. But I am not sure why it is throwing this error. I also tried placing this in assets folder instead and then changed the URI accordingly and ran the program but still I end up with the same error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2024-10-26 04:21:35

您可以将它们放在 res/raw 文件夹中,然后创建资源 URI 指向他们。它们的形式为android.resource://com.example.myapp/raw/my_resource。我不确定其他应用程序(例如 PDF 阅读器)是否能够打开它们。尝试一下!

You can place them in the res/raw folder, and then create resource URIs to point to them. They are in the form of android.resource://com.example.myapp/raw/my_resource. What I do not know for sure is if other apps such as a PDF reader would be able to open them. Try it out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文