Android 在 API 18 上使用 Intent 选择 PDF

标签 android pdf android-intent

我使用以下代码通过 Intent 选择 PDF 文件。它在 Android 5.0+ 上完美运行,但没有合适的应用程序打开 PDF 文件消息出现在 API 18 上。

public static Intent pickPdf() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/pdf");
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    return intent;
}

startActivityForResult(Intent.createChooser(pickPdf(), "Open with"), PICK_PDF);

最佳答案

正如@CommonsWare 所建议的那样——不能保证安装了处理 PDF 的应用程序。

我之前解决这个问题的方法是使用 App Chooser ,像这样:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
    ShowToast(TAG, "Unable to open PDF. Please, install a PDF reader app.");
}   

关于Android 在 API 18 上使用 Intent 选择 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32232412/

相关文章:

android - 服务停止申请最近清除

android - Android 上 Cordova/Phonegap Webview 的自动测试

java - Android studio/Firebase - 遇到无限循环试图获取数据

java - PdfBox 文本提取无法正常工作

c# - 无法让 ImageResizer 调整我的 Pdf 大小

android - 适用于 map 和位智的导航 Intent

android - 工具栏中的自定义布局,下方带有 TabLayout

javascript - 包含 pdf 文件的页面显示 5 分钟后重定向到另一个页面

android - 使用 ACTION_SEND 在 Android 中发布到 Facebook

Android 应用到应用通信