android - 如何通过 Intent 从 SD 卡打开 PDF

标签 android pdf android-intent

我正在尝试启动 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/

相关文章:

android - 为什么 Android+Kotlin 中 newStartIntent 的伴随对象?

java - 更新 JDK 版本以匹配 JRE?

java - Appium:无法在android中的datepicker中选择按钮

iOS Swift 使用 Alamofire 上传 PDF 文件(多部分)

pdf - 用于查看和注释 PDF 文档的组件

java - android onServiceConnected() 是在单独的线程中调用的吗?

android - 如何绘制从我所在位置到目的地位置的路线?

android - 如何在抽屉布局中放置项目事件?

php - 这可能吗?从 PDF 到可编辑表格

android - 电子邮件 Intent 忽略预设文本中的换行符 (Android)