There is a lint warning in AS with regards to android.permission.WRITE_EXTERNAL_STORAGE
. The warning says that the permission will no longer provide write access when targeting Android 10 and above. Removal of the said permission can still write in internal storage folder Pictures/MY_APP_NAME
to save images, but it only works on Android 10 (SDK 29) and/or above (haven't tested yet on Android R). When I tested it again on lower version such as Android M (SDK 23), saving images stop working so I decided to return the android.permission.WRITE_EXTERNAL_STORAGE
thus the warning shows up again. Is it possible that the lint is just false positive that incorrectly diagnosed the problem on different cases? Because currently my support SDK starts with 21 up to the latest which is 30 but the lint only point out that it is no longer needed when targeting Android 10 (SDK 29) and did not consider looking back at the project's minimum SDK support.
A workaround is to actually ignore the warning, as it is just informational and therefore harmless. By setting maxSdkVersion to 28 no need to worry anymore.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"
tools:ignore="ScopedStorage" />
Note that using the android:requestLegacyExternalStorage flag as stated in other answers is not a solution, is just a temporary patch which will no longer work at all in Android 11 (API 30), and future versions
UPDATE, to clarify the doubts and confusions shown by some developers in the comments:
If using the requestLegacyExternalStorage flag in Android 10 (API 29) then request the WRITE_EXTERNAL_STORAGE permission as usual.
The flag requestLegacyExternalStorage does nothing in Android 11 (API 30), it is completely ignored, and there is not workaround for it.
WRITE_EXTERNAL_STORAGE does not give any privileges in Android 11 (API 30), it does nothing at all, therefore in API 11 you need to set the maxSdkVersion to 29.
If in Android 10 (API 29) you are also not using requestLegacyExternalStorage then set maxSdkVersion to 28 instead of 29.
Starting in Android 11 (API 30), the older File API can again be used but "only" when accessing the public "shared storage" folders (DCIM, Music, etc.), or your app "private" directory. For other locations the DocumentFile API is required.
Consider that the File API is now much slower in Android 11 (API 30), because has been refactored becoming essentially a wrapper. This is to enforce its usage just to the allowed locations. So, is no longer a fast system file API, is just a wrapper that internally delegates the work to the MediaStore. When using the File API in Android 11 or above you should consider the performance penalty hit, as according to the Android team it will be 2 to 3 times slower than if accessing directly the MediaStore.