首页 > 解决方案 > 未找到处理 Intent 的 Activity (android.intent.action.VIEW)

问题描述

在我的应用程序中,我有一个 pdf,可以通过选择设备中已安装的应用程序来打印。当我尝试打开应用程序时,它会抛出“找不到处理 Intent 的活动”。我也在我的清单文件中添加了,但它没有帮助。我在用于打印 pdf 的模拟器中安装了 printshare。下面是我的代码。

handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Uri uri = FileProvider.getUriForFile(InventoryPDFCreate.this,getPackageName(), pdfFile);
                                Intent intent = ShareCompat.IntentBuilder.from(InventoryPDFCreate.this)
                                        .setStream(uri) // uri from FileProvider
                                        .getIntent()
                                        .setAction(Intent.ACTION_VIEW) //Change if needed
                                        .setDataAndType(uri, "pdf/*")
                                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                        startActivity(intent); 

                            } catch (ActivityNotFoundException e) {
                                e.printStackTrace();
                                toastMsg.showToast(InventoryPDFCreate.this, "No Application match to Open Print File"+e);
                            }
                        }
                    }, 100);

清单文件

<queries>
        <!-- WebView -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" />
        </intent>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" />
        </intent>

        <!-- Camera -->
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>

        <!-- Gallery -->
        <intent>
            <action android:name="android.intent.action.GET_CONTENT" />
        </intent>
    </queries>

我收到以下异常:

No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.mobilesalesperson.controller/external/Android/AMSP/PrintReport/msp.pdf typ=pdf/* flg=0x80001 (has extras) }

标签: androidexception

解决方案


问题是 OP 使用了不正确的 mime 类型作为参数。Android 不支持所有类型的 mime,但这可以通过使用MimeTypeMap来检查。

如果支持,则返回给定扩展的 MIME 类型,否则返回 null。

final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");

推荐阅读