【问题标题】:No Activity found to handle Intent (android.intent.action.VIEW)未找到处理 Intent 的 Activity (android.intent.action.VIEW)
【发布时间】:2021-06-23 07:29:10
【问题描述】:

在我的应用程序中,我有一个 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) }

【问题讨论】:

  • setDataAndType(uri, "application/pdf")?
  • 请将其作为答案发布。

标签: android exception


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    试试这样的

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    startActivity(intent);
    

    或在您的网页视图中打开 pdf

    WebView webView = findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginsEnabled(true);
    webView.loadUrl("https://docs.google.com/viewer?"+pdf_url);
    

    【讨论】:

    • 感谢您的回复。我的要求是从 Printshare 应用程序打印 pdf。现在已经完成了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多