From Android 10 onward, Google modified the way in which that storage is dealt with. This grew to become obligatory for all apps concentrating on Android 11 and above. The adjustments have been carried out with privateness in thoughts, stopping customers from being pressured to grant entry to each file on their machine. This scoped storage tutorial will inform you what you have to know.
Scoped storage tutorial: The cliff-notes model
With scoped storage, customers might want to grant permission any time an app makes an attempt to entry a file it didn’t create. Builders are additionally inspired to position recordsdata in particular folders, thereby lowering the quantity of muddle and disorganization. Any recordsdata exterior of these folders shall be deleted as soon as an app is eliminated.
See additionally: A information to Android app improvement in 5 simple steps
Scoped storage is granted by default and is predicated on the kind of file being saved (these are organized as “collections”). Apps are solely given entry to the sorts of storage they really use.
In observe, which means that devs not want to make use of the WRITE_EXTERNAL_STORAGE permission, as this has no impact on an app’s entry to storage. Sure issues get simpler, others get much more sophisticated. Learn the remainder of this scoped storage tutorial to seek out out what you have to know.
Accessing app-specific storage
Apps want no particular permissions to entry scoped storage and there are not any adjustments in the way in which that you just save to inner storage. Nonetheless, there are some things to contemplate when utilizing app-specific storage, which we’ll cowl on this scoped storage tutorial.
Credit score: Edgar Cervantes / Android Authority
There are two app-specific areas in exterior storage which ought to primarily be used when inner storage is inadequate. These are designated for the app’s persistent recordsdata and cached recordsdata, respectively.
To entry these areas, the app should first confirm that the storage is offered (availability is assured for inner storage). Question the quantity’s state utilizing Surroundings.getExternalStorageStage(). If MEDIA_MOUNTED is returned, you may learn and write recordsdata to exterior storage.
Subsequent, you have to to decide on a bodily storage location. This may increasingly imply selecting between inner reminiscence vs an SD card, for instance. Name ContextCompat.getExternalFilesDir(). The primary factor within the returned array is taken into account to be the “main” exterior storage choice, and this needs to be used within the majority of instances.
To entry app-specific recordsdata from exterior storage, name getExternalFilesDir().
There are a selection of ready-defined directories meant to assist with logical storage of recordsdata. These embody:
Use these or go “null” if you wish to entry the foundation area for the app-specific listing.
So as to add app-specific recordsdata to the cache in exterior storage, use externalCacheDir. To take away them, use delete().
Accessing different recordsdata
Lastly, to carry this scoped storage tutorial to an in depth, we should contemplate how we entry and write recordsdata which are shared throughout the system.
Apps should select between “app-specific storage” or “shared storage.” The latter is used if you need to share recordsdata with different apps, similar to photographs or paperwork. Builders that want to entry recordsdata exterior their app-specific listing ought to use the MANGE_EXTERNAL_STORAGE permission. You may as well use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent motion to ship customers the settings web page the place they will allow entry in your app.
MANAGE_EXTERNAL_STORAGE grants entry to the contents of the MediaStore.Information desk, the foundation listing of the USB OTG drive and SD card, and all inner directories (besides sdcard/Android and its subdirectories). This restriction contains the app-specific directories of different apps, as these are situated in sdcard/Android/knowledge.
We are able to not use File APIs to straight entry recordsdata. As an alternative, we should use the Storage Entry Framework for choosing recordsdata and folders, and URI’s for media recordsdata. To entry a photograph, for instance, we should now use URIs slightly than MediaStore.Pictures.Media.DATA. This was already the really useful observe, however is now obligatory.
See additionally: Introduction to the Google Play Console for builders
For accessing easy recordsdata with the Storage Entry Framework, there are three principal actions:
These are pretty self-explanatory. For displaying photographs, we might first get the ID of the picture with MediaStore.Pictures.Media._ID after which construct the Uri with ContentUris.withAppendedID. It is a considerably lengthier course of than it was and definitely will increase the learning-curve for what might be a comparatively easy course of.
That wraps up this scoped storage tutorial! What do you consider scoped storage? Is it a obligatory safety replace, or a irritating hurdle for builders?
For extra developer information, options, and tutorials from Android Authority, don’t miss signing up for the month-to-month publication under!