My confusion related to Android storage management-related APIs | by hasib prince | Sep, 2023
I was always confused about storage management-related APIs. When ScopedStorage was introduced confusion increased. The confusion were related to these points:
- Choosing APIs based on use cases
- Needed permissions depending on Android versions
- What actually internal/external storage means
These confusions will be clarified below.
Types of storage:
Firstly, Internal/External Storage, Initially I assumed from the name of Internal storage means built-in memory and External Storage is additional memory like an SD card. Later I came to know, these are app specific storage. Internal Storage is a part of the storage that is not accessible to the users. This storage is only accessible by apps. On the other hand, External storage is accessible by users and other apps based on given permissions.
Note: Built-in memory is called Primary storage and additional SD Card is called Secondary storage.
Now come to the details of app-specific storage, Which is dedicated to apps. We already know that there are two types of app specific storage, one is internal storage, and the other one is external storage.
Internal storage is used to maintain app specific data, like database, preferences, and caches, those are very private data for the app. On Android 10 (API level 29) and higher, these locations are encrypted. These files are not accessible by either other apps or users. Usage of internal storage:
context.dataDir //(For acessing, data directory)
context.cacheDir // (For acessing, cache directory)
Another app-specific storage is external storage where the app keeps data those are accessible to other apps or users. Other apps need appropriate permissions to access these data. The files we want to make accessible by users but will be removed after uninstall or relatively bigger files can be saved in external storage. Usage external storage:
context.externalCacheDir // (For acessing, external cache directory)
When the user uninstalls the app, all the data of app-specific storage (both internal and external) will be deleted. So, any app shouldn’t keep any data to app-specific storage that is important to user. For example, A video editor app is used by the user to edit the video and this edited video shouldn’t be saved in app specific storage. These types of output files should be saved in shared storage. Example of shared storage, the location of Downloads, Movies, Pictures, and Documents directories. So, Here we got the idea about types of storage:
- App Specific Storage
1. Internal Storage
2. External Storage
- Shared Storage
Needed permissions and Choosing apis:
Now let’s come to the point of needed permissions and choosing apis for various storage.
First of all, app specific directory of internal storage, no permission is needed to access this storage.
Then app-specific directory of external storage, no permission is needed if the app runs on android 4.4 (api level 19) or higher, otherwise permission will be needed (READ_EXTERNAL_STORAGE/WRITE_EXTERNAL_STORAGE). Usage of internal and external storage is mentioned above.
We can use MediaStore Api to access shared storage. If we want to add any file, we don’t need any permission for anroid Q and above. But If we want to modify any existing file (edit/delete), we will need permission (READ_EXTERNAL_STORAGE). Usage of MediaStore api.
For below android Q, We have to use older way to add files and we have to add permission as well, for reading READ_EXTERNAL_STORAGE and for writing WRITE_EXTERNAL_STORAGE. Usage of older way:
Lastly, These were my findings to clarify the confusion i faced during i tried to understand storage management apis. I hope it will be helpful for the ones who will face same issues. If anyone has any feedback on this, please mention in comment. Bye Bye…
- Mobile App Development (443)