open failed: EACCES (Permission denied) but only on Android 10

Viewed 34

When downloading a file from URL to the downloads directory it crashes on Android 10 Emulator:

java.io.FileNotFoundException: /storage/emulated/0/Download/shareFile.mp3: open failed: EACCES (Permission denied)

This is how I download the file:

val storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
withContext(Dispatchers.IO) {
    URL(soundURL).openStream().use { input ->
        FileOutputStream(File(storage.absolutePath, "shareFile.mp3")).use { output ->
            input.copyTo(output)
        }
    }
}

It crashes in the 4th line when calling FileOutputStream

I got both permissions granted: READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE

On my Android 11 and 12 Devices everything works fine

1 Answers
Related