Android 从 URI 起始内容打开 PDF

问题描述 投票:0回答:1

我正在尝试使用以下意图打开 PDF:
这是我的代码:

    Intent intent = new Intent(Intent.ActionVIEW)
    intent.setDataAndType(Uri.parse("CONTENTURI + FILENAME","application/pdf"
    try { startActivity(intent)} catch excpetion and so on.

它会弹出我安装的任何应用程序,包括 Adobe Reader、Google PDF 阅读器、POLARIS(因为我使用 Galaxy Tab 3 进行测试),但这些都不起作用。每个都说不支持的文档。

是否下载并显示了这种情况下的正确解决方案或任何想法?预先感谢!

android pdf mime-types
1个回答
0
投票

以下代码触发 pdf 文件的

VIEW
操作(打开默认 PDF 查看器,在能够查看 PDF 的应用程序之间进行选择,如果没有安装,则会出现错误)。

    File file = new File("your pdf path here");

    if (file.exists()) {

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

     try {
     startActivity(intent);
      } catch (ActivityNotFoundException e) {
      //if user doesn't have pdf reader instructing to download a pdf reader 
       }   


    }

注意:PDF 不应保存在应用程序本地,否则第三方应用程序将无法访问。您应该使用

media
位置。

© www.soinside.com 2019 - 2024. All rights reserved.