Android - 未找到处理 Intent { act=android.intent.action.VIEW - 尝试打开 PDF 文件的活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25949802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:52:27  来源:igfitidea点击:

Android - No Activity found to handle Intent { act=android.intent.action.VIEW - Trying to open a PDF File

androidpdfandroid-intentandroid-activity

提问by Rasool Ghana

I searched a lot to find a solution , but still can't find any

我搜索了很多以找到解决方案,但仍然找不到任何解决方案

I am trying to open a PDF file in my android application

我正在尝试在我的 android 应用程序中打开一个 PDF 文件

Here is my code to do this :

这是我执行此操作的代码:

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

The Tomcat Log implies that there is no activity to handle the intent

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 }

Is there any other way to do this or can i open the PDF file with another default PDF viewer? If yes, how?

有没有其他方法可以做到这一点,或者我可以用另一个默认的 PDF 查看器打开 PDF 文件吗?如果是,如何?

Update :the main problem was that i did not install a PDF viewer application on my emulator ...

更新:主要问题是我没有在模拟器上安装 PDF 查看器应用程序...

采纳答案by ChuongPham

Try this instead:

试试这个:

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: The above method only work if you have a pre-installed PDF Viewer. If not, you need to install a PDF Viewer.

注意:上述方法仅适用于预安装的 PDF 查看器。如果没有,您需要安装 PDF 查看器。

回答by nadavfima

You just have to popup some sort of error msg to the user if no activity was found to launch the PDF (In the error dialog you can let the user know he needs some PDF app and send him to the Play Store), though i'd suggest using this piece of code instead of the genral exception catcher you're using:

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

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

or you can use some PDF library, like this one.

或者你可以使用一些 PDF 库,比如这个