The following code is working absolutely fine in devices with API 22 but in devices with API 24 it gives the following error:
java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
Here is my code:
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();
}
}
If I change MODE_WORLD_READABLE to MODE_PRIVATE then it stops working in all devices. devices with API 22 give the following error
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
Device with API 24 throw the following exception
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()
Can someone help me in resolving the issue.
Thanks.