【发布时间】:2015-03-09 20:16:07
【问题描述】:
在我的应用程序中,我想将 PDF 文件存储在资产中,当按下查看按钮时,设备中安装的第 3 方应用程序应查看 PDF。
通过使用以下代码,我尝试访问资产文件夹中的 PDF 文件,但应用显示“文件不可用”。我在 stackoverflow 上搜索了各种代码,但没有一个有效。但是当我尝试提取 APK 文件时,我可以在 assets 文件夹中看到 PDF 文件。
以下是我试过的代码:
File file = new File("file://" + getFilesDir() + "/ccv.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(home.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(home.this,
"File not available",
Toast.LENGTH_SHORT).show();
}
所以请提供代码 sn-p 以访问 assets 文件夹中的 PDF 文件。
提前致谢
【问题讨论】:
标签: android file pdf android-intent android-assets