我在如此基本的事情上碰壁了。我想要做的是使用任何安装的 PDF 阅读器程序(如 Adobe Reader)打开一个 PDF 文件。但是,当我检查注册为“application/pdf”的任何已安装应用程序时,即使我已验证安装了 Adobe Reader,它也总是返回 null。这是我使用的代码:
String pdfFile = "android.resource://" + getPackageName() + "/" + R.raw.middleburychamberweb2014;
Intent pdfIntent = null, chooser=null;
pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.parse(pdfFile), "application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(pdfIntent, 0);
if (pdfIntent.resolveActivity(getPackageManager()) != null) {
chooser = Intent.createChooser(pdfIntent,"Select Viewer for PDF file");
startActivity(chooser);
} else {
/* No PDF reader found on device. Provide user the option to
* install the Adobe Reader found on Google Play.
*/
Log.e("Directory","No PDF viewers!");
noPDFReader(getString(R.string.doc1web));
}
最佳答案
大约有零个应用程序遵循晦涩的 android:resource
方案,这就是它不起作用的原因。
我同意 323go -- 使用 ContentProvider
,such as FileProvider
.这需要零行 Java 代码来在提供者端实现,因为您只需使用 FileProvider
。当您调用 startActivity()
以允许 PDF 查看器访问内容时,它需要一个 list 条目、一个 XML 元数据文件和一行。这比写入外部存储更具前瞻性,而且更安全。
FWIW,我有 a sample project演示了通过 FileProvider
向外部 PDF 查看器提供 PDF。
关于android - 为什么 Intent 的 Android resolveactivity 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27027498/