如何通过 SD 卡中的 Intent 打开 PDF [英] How to open a PDF via Intent from SD card

查看:39
本文介绍了如何通过 SD 卡中的 Intent 打开 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动 Intent 以在我的应用程序的资产文件夹中打开 pdf.我已经阅读了数十篇文章,但仍然卡住了.显然我需要先将 pdf 复制到 SD 卡,然后启动 Intent.还是不行.

I'm trying to launch an Intent to open a pdf inside my assets folder in my app. I've read dozens of posts but am still stuck. Apparently I need to copy the pdf to the sd card first then launch an Intent. It still doesn't work.

我认为问题在于 Intent 启动,所以我只是想打开一个文件example.pdf",我使用此代码复制到 SD 卡上:

I think the problem is the Intent launching so I'm simply trying to open a file "example.pdf" that I copied onto the SD card using this code:

Log.w("IR", "TRYING TO RENDER: " + Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf"), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

try {
    startActivity(intent);
    Log.e("IR", "No exception");
} 
catch (ActivityNotFoundException e) {
    Log.e("IR", "error: " + e.getMessage());
    Toast.makeText(InvestorRelations.this, 
        "No Application Available to View PDF", 
        Toast.LENGTH_SHORT).show();
}

这是我的 LogCat 输出.

This is my LogCat output.

05-10 10:35:10.950: W/IR(4508): TRYING TO RENDER: /mnt/sdcard/example.pdf
05-10 10:35:10.960: E/IR(4508): No exception

除了运行此代码时,我得到以下 Toast(不是由我的应用程序生成)

Except when this code is run, I get the following Toast (not produced by my app)

不支持的文档类型"

但我可以通过安装的 PDF 查看应用程序手动打开文档.任何帮助将不胜感激.

But I can open the document manually through the PDF viewing app installed. Any help would be hugely appreciated.

推荐答案

试试这个代码,显示pdf文件来自/sdcard

Try this code, display pdf file From /sdcard

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

这篇关于如何通过 SD 卡中的 Intent 打开 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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