Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jsericksk/imagescopedstorage

A simple project in Jetpack Compose that uses Android scoped storage in images.
https://github.com/jsericksk/imagescopedstorage

gallery-images jetpack-compose scopedstorage

Last synced: 7 days ago
JSON representation

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

Awesome Lists containing this project

README

        



# Objective

The project's main objective is to explore some of the [scoped storage](https://source.android.com/docs/core/storage/scoped) that was introduced in Android 10. It was originally done in **XML** and then recreated in **Jetpack Compose**. [MediaStore](https://developer.android.com/reference/android/provider/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](https://thispersondoesnotexist.com), [This Cat Does Not Exist](https://thiscatdoesnotexist.com) and [This Horse Does Not Exist](https://thishorsedoesnotexist.com) are used as options to generate images.

You can download the app by going to [releases](https://github.com/jsericksk/ImageScopedStorage/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](https://github.com/jsericksk/ImageScopedStorage/issues/1) 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](https://developer.android.com/guide/topics/providers/document-provider), 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

- [Landscapist](https://github.com/skydoves/landscapist)
- [Accompanist: Navigation Animation, Permissions and Pager](https://github.com/google/accompanist)