Skip to content

A simple project in Jetpack Compose that uses Android scoped storage in images.

Notifications You must be signed in to change notification settings

jsericksk/ImageScopedStorage

Repository files navigation

Objective

The project's main objective is to explore some of the scoped storage that was introduced in Android 10. It was originally done in XML and then recreated in Jetpack Compose. MediaStore is used for most tasks with images. The operations are very similar with videos and audios.

About the app

A simple app was created that uses the functions of save, get and delete images in a defined scope. In other words, a folder called Image Scoped Storage will be created in the Pictures directory on the device storage.

The websites This Person Does Not Exist, This Cat Does Not Exist and This Horse Does Not Exist are used as options to generate images.

You can download the app by going to releases if you want to test it on your device.

Issues on Android 10

Apparently, the first version of Android to receive this new feature didn't handle scoped storage very well. When the file is not created by the app or when the user reinstalls or even deletes the app's data, the Android system throws a security exception for the user to manually confirm the deletion of the file. There is a known issue that only occurs on Android 10 where the file is not deleted when the user confirms the deletion or even after doing a second delete with methods like contentResolver.delete(uri, null, null).

It is also possible to notice that in Android 10 it is not possible to delete multiple files at once without the user having to manually confirm with the standard dialog that is shown after a SecurityException is thrown, something that has been "fixed " in Android 11 with the introduction of the MediaStore.createDeleteRequest(...) function, where you can pass a list of Uris.

Because of these issues, the most recommended solution is to disable scoped storage on Android 10 with android:requestLegacyExternalStorage="true". With this, no SecurityException will be thrown when trying to delete media files. However, it's important to remember that this will only work on Android 10, as starting with Android 11, this property is ignored and scoped storage is enabled by default. Another less attractive solution is to use Storage Access Framework, but in most cases, like this project, it ends up generating more work than than necessary, which brings us to the solution mentioned above.

Test SecurityException

If you want to test it yourself and see the system default dialog for file deletion on Android 11 and up, you can:

  1. Save some images and erase app data. After trying to delete some image, the exception will be thrown.
  2. Move some images to the Image Scoped Storage folder. After trying to delete some image, the exception will be thrown, as the images were not generated by the app.

Libraries used

About

A simple project in Jetpack Compose that uses Android scoped storage in images.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages