我希望我的应用程序打开pdf

时间:2012-03-29 01:40:54

标签: android file pdf

我的应用程序将一个pdf文件从保存到SD卡的链接下载,并且它工作正常..但我正在努力的是我无法让我的应用程序查看pdf文件..(虽然我已经在我的模拟器上安装PDF查看器)下载文件后没有任何反应... 这是我打开pdf文件的代码

                                   InputStream input = new BufferedInputStream(url.openStream());
            File sdCard = Environment.getExternalStorageDirectory();
            File dir = new File(sdCard.getAbsolutePath()
                    + "/myapp/download");
            dir.mkdirs();
            File file = new File(dir, "application/pdf");
            FileOutputStream output = new FileOutputStream(file);

1 个答案:

答案 0 :(得分:2)

使用以下代码打开pdf文件。

File pdfFile = new File("/mnt/sdcard/path_to_file/yourpdf.pdf"); 
    if(pdfFile.exists()) {
        Uri path = Uri.fromFile(pdfFile); 
        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(path, "application/pdf");
        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
           startActivity(pdfIntent);
        } catch(ActivityNotFoundException e) {
           Toast.makeText(yourActivityClass.this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
        }
    } else {
        Toast.makeText(yourActivityClass.this, "File not found", Toast.LENGTH_LONG).show(); 
    }

yourActivityClass.this 是应用程序上下文,在测试上述代码时使其正确。