gpt4 book ai didi

android从 Assets 文件夹访问pdf文件

转载 作者:行者123 更新时间:2023-11-30 02:26:04 24 4
gpt4 key购买 nike

在我的应用程序中,我想将 PDF 文件存储在 Assets 中,当按下查看按钮时,设备中安装的第 3 方应用程序应该可以查看 PDF。

通过使用以下代码,我尝试访问 Assets 文件夹中的 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();
}

所以请提供代码 fragment 以访问 assets 文件夹中的 PDF 文件。

提前致谢

最佳答案

Try this

File file = new File("file:///android_asset" + "/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();
}

关于android从 Assets 文件夹访问pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27895639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com