我必须将 pdf 文件与我的应用程序一起发送,并将它们显示为列表,单击时每个文件都应在 Adobe 阅读器中打开。我尝试将 pdf 文件放在/assets 文件夹中,也放在/res/raw 文件夹中。但无论哪种情况,我都会遇到 ActivityNotFound 异常。代码和异常消息如下。
将文件放在/assets/pdf 文件夹中
try {
files = amanager.list("pdf");
//AssetFileDescriptor fd = amanager.openFd(files[0]);
Uri path = Uri.parse("android.resource://com.csg.android.myproject/assets/pdf/csgsample") ;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage("com.adobe.reader");
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();
}
将文件放在/res/raw 文件夹中
Uri.parse("android.resource://com.csg.android.myproject/raw/csgsample") ;
异常(exception):
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 pkg=com.adobe.reader }
如有任何帮助,我们将不胜感激。
提前致谢, 纳文
最佳答案
如果你得到一个 ActivityNotFoundException
是因为没有应用程序来处理那种内容。因此,在这种情况下,我确定已安装 Adobe Reader。
此外,我认为您必须将文件复制到 SDCard 以便其他应用程序访问它。出于安全原因,他们无法直接访问您的私有(private)数据。
关于android - 在 Android 中读取 Assets 或原始文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5120790/