如何打开以前存储在“私有”文件系统中的文件?该文件正在由网络服务下载,并应存储在本地文件系统上。
尝试通过意图(Action_View)打开文件时出现“奇怪”错误。尽管该文件存在于文件系统中(在模拟器/Eclipse 的文件资源管理器中看到该文件),但它不会显示在启动的调用画廊活动中。画廊显示的不是图片,而是黑屏,其中没有内容(操作栏除外)。当尝试通过此方法打开 pdf/电影/mp3 文件时,也会发生同样的情况(例如 pdf 表示文件已损坏)。
顺便说一句:存储在本地文件系统上的数据是有效的(未损坏),我从调试器下载了文件(通过拉取方法),并且可以在我的工作站上打开文件......
public void onAttachment(Context context, Attachment attachment) {
try {
//Attachment is a structured data object that contains of a byte[], filename and mimetype (like image/jpeg)
FileOutputStream fos = FileOutputStream fos = context.openFileOutput(attachment.getAttachmentFileName(), Context.MODE_PRIVATE);
fos.write(attachment.getBinary());
fos.flush();
fos.close();
File f = context.getFileStreamPath(attachment.getAttachmentFileName());
Uri uri = Uri.fromFile(f);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,attachment.getAttachmentMimetype());
context.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
您的代码中附件的类型是什么?也许它返回错误的 mime 类型?
这段代码对我有用:
File file = new File("EXAMPLE.JPG");
// Just example, you should parse file name for extension
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(".JPG");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mime);
startActivityForResult(intent, 10);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)