Android 模拟器 - Intent.createChooser 在打开 pdf 类型文档时显示 "No application can perform this action"

标签 android pdf android-intent android-emulator

我需要要求用户从设备内部/外部存储器中选择一个pdf文档,下面是我正在使用的代码。 它适用于真实设备,但不适用于模拟器我已在模拟器上安装了 Pdf 查看器。

它不会引发错误,但会显示消息框窗口,显示“没有应用程序可以执行此操作”

     Intent intent = new Intent();
     intent.setType("pdf/*");
     //intent.setType("application/pdf");
     intent.setAction(Intent.ACTION_GET_CONTENT);


     try {
         Intent pdfIntent = Intent.createChooser(intent, "Select pdf");
         startActivityForResult(pdfIntent, SELECT_PDF_DIALOG);
 } 
 catch (ActivityNotFoundException e) {
     CommonMethods.ShowMessageBox(this, "No Application Available to View PDF.");
 }
 catch(Exception e)
 {
     CommonMethods.ShowMessageBox(this, e.toString());
 }

我需要此代码才能在模拟器上运行,因为我无法检查/测试应用程序的完整功能。

感谢您的帮助。

最佳答案

这是因为 Android 不提供任何内置功能来读取 .pdf 扩展名。因此,您必须安装任何第三方应用程序来阅读 .pdf(任何 PDF 查看器)才能使其正常工作。

好吧,那就试试这个吧,

将你的 PDF 文件保存在 SDCard 中并尝试用这个东西执行它,

            File file = new File("/sdcard/Android.pdf");

            if (file.exists()) {
                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                } 
                catch (ActivityNotFoundException e) {
                    Toast.makeText(YourActivity.this, 
                        "No Application Available to View PDF", 
                        Toast.LENGTH_SHORT).show();
                }
            } 

关于Android 模拟器 - Intent.createChooser 在打开 pdf 类型文档时显示 "No application can perform this action",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10152252/

相关文章:

android - 在 setVisible 设置为 false 然后 true 之后,ActionBar 图标不再出现在 ActionBar 上

android - 没有移动塔的移动到移动通信?

android - 如何在 Android Studio 上冷启动运行 API 27+ 的模拟器?

c# - 如何使用相对文件路径和 PDFsharp 创建 PDF 文件链接?

pdf - PDF 文件的结构?

android - android中的USB接口(interface)

c# - wkhtmltopdf 不会在 html 文件中拍摄图像

android - 无法在 Intent : The Method Put Extra is Ambiguous for the type Intent 中传递自定义对象

android - 如何完成应用程序并返回上一屏幕

android - ActionBar 菜单项的不同行为