2014-09-20 85 views
7

我搜索了很多找到一个解决方案PDF文件,但仍无法找到任何Android的 - 无活动处理意向{行动= android.intent.action.VIEW - 试图打开

我想在我的Android应用程序

这里打开一个PDF文件是我的代码做到这一点:

try 
    { 
     File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf"); 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     Uri uri = Uri.fromFile(file); 
     Log.d("CheckingURI", uri.toString()); 
     intent.setDataAndType(uri, "application/pdf"); 
     startActivity(intent); 
    } 
    catch (Exception e) 
    { 
     Log.d("OpenPDFError", e.getMessage()); 
    } 

Tomcat的日志意味着没有任何活动,以处理这个意图

09-19 19:55:02.938: D/CheckingURI(30483): file:///mnt/sdcard/pdf/Read.pdf 
09-19 19:55:02.948: D/OpenPDFError(30483): No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf } 

是否有任何其他方式来做到这一点,或者我可以打开PDF文件与另一个默认的PDF查看器?如果是,如何?

更新: 的主要问题是,我没有在我的仿真器上安装一个PDF阅读器应用程序...

回答

7

试试这个:

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

警告:以上方法只工作如果您有预先安装的PDF查看器。如果不是,则需要安装PDF查看器。

+0

再次出现此错误:未找到处理Intent的活动{act = android.intent.action.VIEW dat = file:///mnt/sdcard/pdf/Read.pdf typ = application/pdf flg = 0x40000000} – 2014-09-20 14:37:47

+1

@RasoolGhana:也许你的设备或模拟器上没有安装PDF阅读器。尝试安装PDF查看器并重试。 – ChuongPham 2014-09-20 15:11:49

+0

谢谢,一直在寻找这个。 +1有关“未找到活动...”错误的说明。 – 2014-09-20 16:43:17

2

如果找不到任何活动来启动PDF,您只需向用户弹出某种错误消息(在错误对话框中,您可以让用户知道他需要一些PDF应用程序并将其发送到Play商店)虽然我建议使用这段代码,而不是你所使用的GENRAL例外捕手:

try { 
    /* your code */ 
    ... 
} catch (ActivityNotFoundException e) { 
    e.printStackTrace(); 
} 

,或者你可以使用一些PDF库,像this one

相关问题