我使用以下代码通过 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/