android - 未找到处理 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) }
解决方案
问题是 OP 使用了不正确的 mime 类型作为参数。Android 不支持所有类型的 mime,但这可以通过使用MimeTypeMap来检查。
如果支持,则返回给定扩展的 MIME 类型,否则返回 null。
final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
推荐阅读
- javascript - 如何验证具有多个选项卡的表单?
- angular - Angular - RxJS - Observable 和方法返回
- amazon-web-services - 访问 S3 中数据的权限不足 - AWS
- html - 背景过滤器会导致字体上不需要的“抗锯齿”
- amazon-web-services - EC2实例向外发出请求时如何查找EC2实例使用的IP地址
- mysql - 尝试将变量作为表名和值插入时出现 mysql 语法错误
- javascript - 带有循环的 Promise.all() 并将结果同时存储到 Javascript 中的 JSON 对象
- .net - 在 WinDbg 中从 .NET 1.1 内存转储中转储所有字符串
- javascript - 如何防止 React 重新渲染?
- android - 使用 arraylist 将数据添加到微调器中,而不是在 addChildEventListener 的 onChildChanged() 中更新