Permission denied on writing to external storage despite permission

UPDATE: See Volker Voecking's answer for a better solution


EDIT: This is simply a workaround for those who don't have time to look for a solution. *

If you get permission denied error even when the permissions are granted and you already implemented permission checks,

make sure you're not targetting api level 29:

Change targetSdkVersion and compilesdkversion from 29 to 28 or any other lower level.


If you target SDK 29 and you still get a "permission denied" error after successfully requesting the WRITE_EXTERNAL_STORAGE permission you should add

android:requestLegacyExternalStorage="true"

to the application definition in AndroidManifest.xml.

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
    <application android:requestLegacyExternalStorage="true" ... >
      ...
    </application>
</manifest>

see https://developer.android.com/training/data-storage/compatibility