I've got an old app that uses the following lines to download an APK into the devices downloads directory:
File downloadsDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File outputFile = new File(downloadsDirectory, "appname.apk");
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
// ...
I don't know for sure if those lines ever worked, but according to the client it did. So, now I'm stuck with maintaining that app and I'll have to figure out a way to make it working again. With the above lines I get the following error message:
E/my.appname.services.UpdateService: downloading of latest release failed
my.appname.exceptions.NetworkException: java.io.FileNotFoundException: /storage/emulated/0/Download/appname.apk (Permission denied)
at my.appname.services.UpdateRepository.download(UpdateRepository.java:89)
at my.appname.services.UpdateService.onUpdateFound(UpdateService.java:78)
at my.appname.services.UpdateService.checkForUpdates(UpdateService.java:61)
at my.appname.services.UpdateService.onHandleIntent(UpdateService.java:48)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
Caused by: java.io.FileNotFoundException: /storage/emulated/0/Download/appname.apk (Permission denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:287)
at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at my.appname.services.UpdateRepository.download(UpdateRepository.java:81)
at my.appname.services.UpdateService.onUpdateFound(UpdateService.java:78)
at my.appname.services.UpdateService.checkForUpdates(UpdateService.java:61)
at my.appname.services.UpdateService.onHandleIntent(UpdateService.java:48)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
Given that the app is quite old, I immediately suspected some missing permissions and indeed there was no android.permission.WRITE_EXTERNAL_STORAGE
and android.permission.READ_EXTERNAL_STORAGE
entires in the manifest. But even after adding those I still get the same error message.
Any ideas, what else might be missing to get access to the downloads directory?
As comment above I will quote it as answer
If you are running on Android 6.0 Marshmallow (API 23) and a above you should request runtime permission