我需要要求用户从设备内部/外部存储器中选择一个pdf文档,下面是我正在使用的代码。它适用于真实设备,但不适用于模拟器。我已经在模拟器上安装了 Pdf 查看器。
它不会引发任何错误,但会显示消息框窗口,指出"没有应用程序可以执行此操作"
Intent intent = new Intent();
intent.setType("pdf/*");
//intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
Intent pdfIntent = Intent.createChooser(intent, "Select pdf");
startActivityForResult(pdfIntent, SELECT_PDF_DIALOG);
}
catch (ActivityNotFoundException e) {
CommonMethods.ShowMessageBox(this, "No Application Available to View PDF.");
}
catch(Exception e)
{
CommonMethods.ShowMessageBox(this, e.toString());
}
我需要此代码在模拟器上运行,因为我无法检查/测试应用程序的全部功能。
感谢您的帮助。
这是因为Android没有提供任何内置功能来读取.pdf
扩展程序。因此,您必须安装任何第三方应用程序才能读取.pdf
(任何PDF查看器)才能使其正常工作。
好的,那就试试这个,
将您的PDF文件保存在SDCard中,并尝试使用此内容执行它,
File file = new File("/sdcard/Android.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(YourActivity.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
您需要在设备或模拟器上安装一些pdf查看器才能完成此操作。