Migrating a Flutter android (API 29) app's downloaded media(images, videos, PDF) to confirm to Scoped Storage of Android 11 (API Level 30)

Viewed 1498

We have a flutter app similar to Whatsapp (say MessagingApp) for broadcasting messages with media (images, videos, PDF) within a closed group of people.

The current flutter app's android targetSdkVersion is 29 and uses the Legacy Storage using file path access method to read and write media files as described below.

All the app's media files are downloaded under the base directory /storage/emulated/0/MessagingApp.

Following are the example file path's of various media:

Sample Downloaded Image's path /storage/emulated/0/MessagingApp/MessagingApp_Images/20210410_111512.jpg

Sample Downloaded Video's path /storage/emulated/0/MessagingApp/MessagingApp_Videos/20210410_111513.mp4

Sample Downloaded PDF's path /storage/emulated/0/MessagingApp/MessagingApp_Documents/20210410_11151.pdf

To download an image the dio package is used:

dio.download(imageHttpUrl, '/storage/emulated/0/MessagingApp/MessagingApp_Images/20210410_111512.jpg',)

The downloaded image is rendered in the app using the Image.file widget: Image.file(File('/storage/emulated/0/MessagingApp/MessagingApp_Images/20210410_111512.jpg'))

The downloaded images can be viewed from android's Gallery app.

Starting November 2021, Playstore forces the targetSdkVersion to be 30 and in the API Level 30's Scoped Storage model, apps cannot create folders directly in the "Shared Storage".

In Scoped Storage the files can be downloaded and used from the app specific directory - /storage/emulated/0/Android/data/org.test.MessagingApp/files But they will not accessible to other(Gallery) apps.

Other than that Scoped Storage mentions to use "MediaStoreAPI" for managing media.

How to manage and handle downloaded media in Flutter android (API 30) with the new Scoped Storage of Android 11?

Also i noticed on emulator running android 11 and app with targetSdkVersion 30, the app cannot create a folder in the root storage /storage/emulated/0/, but the android folders "Documents, Download, DCIM, Pictures and so on" are freely accesible, that is new folders can be created under them using the existing code i.e. the image path can be changed to /storage/emulated/0/Documents/MessagingApp/MessagingApp_Images/20210410_111512.jpg and it works.

What is the best practice for using the new Scoped Storage of Android 11 for a Flutter android app?

0 Answers
Related