Im basically trying to download a file from an url.So i decided to store in my app file.So I used getExternalFilesDir, so that i don't require any permission pop up as per these links:
Do I need runtime permission check to write into getExternalFilesDir() path in marshmallow?
getExternalFilesDir permissions
The Death of External Storage: The End of the Saga(?)
However, it works on android 8.0, but on my Letv Le 1s Marshmellow device without storage permission, it doesn't work!
Below is my code:
if(TextUtils.isEmpty(urlString))
return;
URL u = new URL(urlString);
is = u.openStream();
dis = new DataInputStream(is);
byte[] buffer = new byte[1024];
int length;
File storageDir = new File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_MOVIES).toString(), "downloads/");
String fileNameFromUrl = urlString.substring(urlString.lastIndexOf("/")+1,urlString.length());
String ext = urlString.substring(urlString.lastIndexOf(".") + 1, urlString.length());
storageDir.mkdirs(); // make sure you call mkdirs() and not mkdir()
File downloadPath = new File(storageDir + File.separator + fileNameFromUrl);
if(downloadPath.exists()){
downloadPath.delete();
}
downloadPath.createNewFile();
downloadPath is /storage/emulated/0/Android/data/com.example/files/Movies/downloads/file.mp4
This line downloadPath.createNewFile(), throws java.io.IOException: open failed: EACCES(Permission denied)
My idea is to use the application's directory for reading and writing files for in-app uses and not to ask for storage permissions.So I used context.getExternalFilesDir.
I have explored many SOF links as mentioned above, but it didn't help me solve the issue!