我正在尝试启动 Intent
以在我的应用程序的 Assets 文件夹中打开一个 pdf。我已经阅读了几十篇文章,但仍然卡住了。显然我需要先将 pdf 复制到 sd 卡,然后启动 Intent
。还是不行。
我认为问题在于 Intent
启动,所以我只是尝试打开使用此代码复制到 SD 卡上的文件“example.pdf”:
Log.w("IR", "TRYING TO RENDER: " + Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf"), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
Log.e("IR", "No exception");
}
catch (ActivityNotFoundException e) {
Log.e("IR", "error: " + e.getMessage());
Toast.makeText(InvestorRelations.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
这是我的 LogCat 输出。
05-10 10:35:10.950: W/IR(4508): TRYING TO RENDER: /mnt/sdcard/example.pdf
05-10 10:35:10.960: E/IR(4508): No exception
除非运行此代码,否则我会得到以下 Toast(不是由我的应用生成)
“不支持的文档类型”
但我可以通过安装的 PDF 查看应用手动打开文档。任何帮助将不胜感激。
最佳答案
试试这个代码,显示来自/sdcard的pdf文件
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
关于android - 如何通过 Intent 从 SD 卡打开 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10530416/