Understanding Storage System To Store Data In Android
While making an Android application, we make various variables for storing various data that are used in the application. For example, we can store the data received from some remote database in a variable and we can use that variable throughout the application. But these variable are in-app storage i.e. these will be available to you when the application is running. Once the application is stopped, then all the data stored in the variable will be erased and you will have nothing. Again when you start the application, then those variables will be created again and new values can be stored again in those variables.
To store the data permanently(until deleted) for future reference, we use some kind of storage in Android. These storage systems are called Android Storage System . Some of the storage that Android provides is internal storage , external storage , shared preferences , database , shared storage . But people often get confused about when to use which storage? So, in this blog, we will learn which storage unit should be used when? Let's get started with internal storage.
Internal Storage
When you install some application on your phone, then the Android system will provide you with some kind of private internal storage where the application can store its private data. This data can't be accessed by any other application. When you uninstall the application, then all the data related to that application will be deleted.
In order to save some file in the internal storage, you have to get the file from the internal directory. In order to do so, you can use the getFilesDir() method or getCacheDir() method. The getFilesDir() method returns the absolute path to the directory on the filesystem where files are created. The getCacheDir() returns the absolute path to the application-specific cache directory on the filesystem.
When to use Internal Storage?
When you want to have some private data for your application, then you can use internal storage. Another thing that is to be noted here is that if your application is storing some data that can be used by other application, then you shouldn't use internal storage because when you uninstall the app, all your data will be deleted and other apps will never get that data. For example, if your application is downloading some pdf or storing some image or video that can be used by other applications, then you shouldn't use the internal storage.
External Storage
Most of the Android devices have very less internal storage. So, we use some kind of external storage unit to store our data. These storage unit are accessed by everyone i.e. it can be accessed by all the applications in your device. Also, you can access the storage just by connecting the mobile device to some PC.
In order to get access to the external storage, you have to take the READ_EXTERNAL_STORAGE permission from the user. So, any application having this permission can access your application's data.
When to use External Storage?
If the data that is being stored by your application can be used by other application, then you can use external storage. Also, if the file stored by your application is very large as in case of video, then you can store the file in the external storage. If you want the data even after uninstalling the application, then you can use external storage.
If your app is storing some private data that is of no use when the app is not there on the phone, then you should avoid using the external storage.
Shared Preferences
If you have a small amount of data to store and you don't want to use the internal storage, then you can use the shared preferences. Shared Preferences are used to store the data in the form of key-value i.e. you will be having one key and based on that key, the corresponding data or value will be stored.
The data stored in the shared preferences will remain with the application until the application is present on your mobile phone. If you uninstall the application, then all the shared preferences will be removed from the device.
When to use Shared Preferences?
When the data that you want to store is very small, then you can use the shared preference in your application. It is recommended not to store more than 100kb of data in shared preferences.
Also, if you want to store small and private data, then you can use the shared preferences in Android.
Database
Databases are organised collection of data that are stored for future reference. You can store any kind of data in your Database by using some Database Management System. All you need to do is just create the database and do all the operation i.e. insertion, deletion, searching, etc with the help of one query. You will pass the query and the database will return the desired output from the database. SQLite database is one such example of a database in Android.
When to use Database?
When you want to store some structured data, then you can use a database. You can store any kind of data in a database. So, if the data size is very big and you want to access the data in a very easy way, then you can use a database and store your data in a structured format.
Shared Storage
Every application in the device has some private storage in the internal memory and you can find this in android/data/your_package_name directory. Apart from this internal storage, the rest of the storage is called the Shared Storage i.e. every application with the storage permission can access this part of the memory. This includes media collections and other files of different applications.
In applications that store some data that is being used by some other application, we can use shared storage. But in many cases, other applications only need a small part of the shared storage and rest of the storage is of no use for other applications. So why to provide them access to full shared storage? To solve this, the concept of scoped storage is introduced in Android 10.
Learn more about Scoped Storage.
When to use Shared Storage?
When the data stored by your application can be accessed by some other application then you can use shared storage. For example, the images stored by your app can be viewed or edited by some other application present in your phone. Similarly, the pdf stored or generated by your application can be viewed from other applications also. So, in these cases, you can use shared storage.
That's it for this blog.
In this blog, we learned about various Android Storage System that can be used at various places for storing the data of our application. Hope you learned something new today.
Have a look at our Interview Kit for company preparation.
Do share this blog with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website .
Apply Now: MindOrks Android Online Course and Learn Advanced Android
Happy Learning :)
Team MindOrks!
Also, Let’s connect on Twitter , Linkedin , Github , and Facebook