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

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

问题描述

我试图启动一个意图在我的应用程序打开里面我的资产文件夹中的PDF文件。我读过几十个帖子,但我仍然坚持。显然,我需要将PDF复制到SD卡头,然后启动一个意图。它仍然无法正常工作。

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.

我认为这个问题是意图启动,所以我只是试图打开一个文件为例.pdf,我复制到SD卡中使用此code:

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

除了当此code运行时,我得到以下吐司(不是我的应用程序产生的)

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

不支持的文件类型

不过,我可以通过安装在PDF查看应用程序手动打开该文件。任何帮助将是巨大的AP preciated。

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

推荐答案

试试这个code,显示PDF文件在/ SD卡

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卡意图打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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