在 Android 中读取 asset 或 raw 文件夹中的文件
我必须随我的申请一起发送 pdf 文件,并将它们显示为列表,单击时每个文件都应在 Adobe reader 中打开。我尝试将 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") ;
例外:
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 }
任何帮助将不胜感激。
提前致谢, 纳文
I have to ship pdf files along with my application and display them as list and when clicked each one should open in Adobe reader. I have tried placing the pdf files in /assets folder also in /res/raw folder. But in either case I am encountering ActivityNotFound Exception. The code and the exception message are below.
Placing file in /assets/pdf folder
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();
}
Placing file in /res/raw folder
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 }
Any help would be greatly appreciated.
Thanks in Advance,
Navin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您收到
ActivityNotFoundException
是因为没有应用程序可以处理此类内容。因此,在这种情况下,我确信 Adobe Reader 已安装。另外,我认为您必须将文件复制到 SDCard 以便其他应用程序访问它。出于安全原因,他们无法直接访问您的私人数据。
If you get an
ActivityNotFoundException
is because there are no application to handle that kind of content. So, in that case, I'm sure the the Adobe Reader is installed.Also, I think you must copy the file to the SDCard for other apps to access it. They cannot access your private data directly for security reasons.
我认为资产和原始文件只能由您的应用程序查看。第三方应用程序(无论是否捆绑)无法访问这些文件。在打开文件之前,您应该首先将文件放入设备的 SD 卡中。
I think assets and raw files are only viewable by your application. 3rd party applications (bundled or not) cannot access these files. You should first put your files in the device's sdcard before opening them.
我有同样的问题,但我应该注意,这是我通过资源 id 打开的方式
I have the same problem, but I should note that this is how I open by resource id