【发布时间】:2015-05-17 12:36:19
【问题描述】:
我正在尝试在另一个查看器(例如图库或 PDF 查看器)中打开我在一个应用上下载的文件。这是我保存文件的方式:
FileOutputStream fos = null;
try {
fos = from.openFileOutput(name, Context.MODE_WORLD_READABLE);
fos.write(data); //data is my byte[]
fos.flush();
fos.close();
}catch...
// at this point the file IS written to the path. I can SFTP to my [rooted] device
// and can download the file, it's valid, it opens on my computer.
File outputFile = new File(from.getFilesDir().getAbsolutePath() + "/" + name);
if(!outputFile.exists()){
//show some error message
//we are NOT entering here, just a programmatic check that it exists.
return;
}
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension("." + extension.toUpperCase()); //extension is file extension as String
//if it doesn't work, I set it manually to some valid one by looking at the extension myself.
Intent intent = new Intent();
intent.setDataAndType(Uri.parse(outputFile.getAbsolutePath()), mime);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
我可以看到打开的对话框(它是正确的,例如,为 PNG 文件显示画廊,或者如果是 PDF 等,则显示 Adobe Reader),并且活动正确打开。但是,他们无法访问该文件。为什么他们无法访问该文件?
【问题讨论】:
-
MODE_WORLD_READABLE已被弃用,从一开始就不是一个好主意。FLAG_GRANT_READ_URI_PERMISSION与文件权限无关,而是与ContentProvider权限有关。我建议您添加aFileProvider来提供文件。 -
您将应用程序存储在应用程序的私有内存中。我认为您必须将文件存储在 SD 卡中,以便其他应用程序可以访问它。内存,您的应用程序私有,其他应用程序无法访问。
-
@CommonsWare 我最终得到了
FileProvider,谢谢。
标签: android android-4.0-ice-cream-sandwich