Android中打开PDFAndroid、PDF

2023-09-13 02:18:15 作者:我自称情圣!

我想从我的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
    }
}