以下代码在具有 API 22 的设备中运行良好,但在具有 API 24 的设备中会出现以下错误:
java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
这是我的代码:
private void CopyReadAssets(String filename) {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), filename);
try {
in = assetManager.open(filename);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
startActivity(intent);
} catch (Exception e)
{
Log.e("cra",e.toString());
Toast.makeText(PdfFilesList.this, "cra: "+e.toString(), Toast.LENGTH_SHORT).show();
}
}
如果我将 MODE_WORLD_READABLE 更改为 MODE_PRIVATE 那么它会停止在所有设备中工作。使用 API 22 的设备会出现以下错误
11-16 12:00:53.133 16531-31103/? E/DisplayData: openFd: java.io.FileNotFoundException: Permission denied
11-16 12:00:53.134 16531-31103/? E/PdfLoader: Can't load file (doesn't open) Display Data [PDF : 818 New Jeevan Nidhi.pdf] +FileOpenable, uri: file:///data/data/com.user.plansmart/files/818%20New%20Jeevan%20Nidhi.pdf
具有 API 24 的设备引发以下异常
11-16 12:05:00.100 2682-2682/com.user.plansmart E/cra: android.os.FileUriExposedException: file:///data/user/0/com.user.plansmart/files/827%20Jeevan%20Rakshak.pdf exposed beyond app through Intent.getData()
有人可以帮我解决这个问题。
谢谢。