我通过将 PDF 文件放在assets
我的 android 应用程序的文件夹中来发送它们。我可以使用AssetManager
. 我需要在屏幕上显示它们,我可以做到。但我需要使用 pdfviewer 打开它们。
要为每个项目写一个意图onclick()
,我需要传递 PDF 文件的 URI。并且使用AssetManager
我无法获取文件的 URI。有没有更好的方法可以处理这整个事情?
即使我们能够检索到 URI,PDF 查看器是否能够读取 assets 文件夹中的 PDF 文件?
更新:
根据以下建议,我编写了以下代码
try {
Uri path = Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample") ;
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(FirstTab.this, "NO Viewer", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
它抛出一个ActivityNotFoundException
带有以下异常消息:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.csg.android.myproject/assets/pdf/csgsample typ=application/pdf flg=0x4000000 }
我查询了使用的设备PackageManager
,发现安装了 Adobe 阅读器。但我不确定它为什么会抛出这个错误。我也尝试将它放在assets
文件夹中,然后相应地更改 URI 并运行程序,但我最终还是遇到了同样的错误。