Home > Back-end >  open failed: EACCES (Permission denied) but only on Android 10
open failed: EACCES (Permission denied) but only on Android 10

Time:09-15

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

CodePudding user response:

If you target Android 10, you need to set the value of requestLegacyExternalStorage to true in your app's manifest file:

    <application
        android:requestLegacyExternalStorage="true"
    </application>

For more information, you can see here:

https://developer.android.com/training/data-storage/use-cases#opt-out-in-production-app

  • Related