【问题标题】:Android - No Activity found to handle Intent { act=android.intent.action.VIEW - Trying to open a PDF FileAndroid - 未找到处理 Intent { act=android.intent.action.VIEW - 尝试打开 PDF 文件的活动
【发布时间】:2014-11-14 23:39:58
【问题描述】:

我找了很多解决办法,但还是找不到

我正在尝试在我的 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 查看器应用程序......

【问题讨论】:

    标签: android pdf android-intent android-activity


    【解决方案1】:

    如果没有找到启动 PDF 的活动,您只需向用户弹出某种错误消息(在错误对话框中,您可以让用户知道他需要一些 PDF 应用程序并将他发送到 Play 商店),虽然我建议使用这段代码而不是您正在使用的一般异常捕获器:

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

    或者您可以使用一些 PDF 库,例如 this one

    【讨论】:

      【解决方案2】:

      试试这个:

      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);
      

      CAVEAT:上述方法仅在您预先安装了 PDF 查看器时才有效。如果没有,您需要安装 PDF 查看器。

      【讨论】:

      • 再次收到此错误:未找到处理 Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf 的活动flg=0x40000000 }
      • @RasoolGhana:也许您的设备或模拟器上没有安装 PDF 查看器。尝试安装 PDF 查看器,然后重试。
      • 谢谢,一直在找这个。 +1 有关“未找到活动...”错误的警告说明。
      猜你喜欢
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多