Android中打开PDF [英] Open PDF in Android

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

问题描述

我想从我的Andr​​oid应用程序中打开PDF文件。我搜索如何做到这一点的网络,而且它似乎很容易,但它不工作,至少在我的手机(索尼Xperia P)。

I would like to open a PDF file from my android application. I've searched how to do it in internet, and it seems very easy, but it doesn't work, at least in my mobile (Sony XPeria P).

File file = ....

Intent intent = new Intent(Intent.ACTION_VIEW, 
                  Uri.fromFile (file));             
intent.setType("application/pdf");                  
startActivity(intent);

在执行此code,打开一个窗口,要求选择一个应用程序来显示PDF。当我选择了Adobe Reader的,它是由任何文件显示打开。

When this code is executed, a window is opened asking to choose an application to show the PDF. When I choose the Adobe Reader, it's opened by no document is shown.

我在做什么错了?

推荐答案

试试这个,它的工作对我来说

Try this, Its working for me

//方法来打开的PDF查看器的PDF文件

//Method to open the pdf file in the PDF Viewer

public void OpenPDFFile() {
    File pdfFile = new File(Environment.getExternalStorageDirectory(),"PdfFile.pdf");//File path
    if (pdfFile.exists()) //Checking for the file is exist or not
    {
        Uri path = Uri.fromFile(pdfFile);
        Intent objIntent = new Intent(Intent.ACTION_VIEW);
        objIntent.setDataAndType(path, "application/pdf");
        objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(objIntent);//Staring the pdf viewer
    }
}

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

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