Practically every non-minor application should store information somehow. This information can be of various structures, for example, client settings, application settings, client information, pictures, or a reserve of information got from the web. Some applications may create information that at last has a place with the client, thus, would like to store the information (maybe records or media) in a public spot that the client can access at whenever, utilizing other applications. Other applications should store information, yet don’t need this information to be perused by other applications (or even the client). The Android stage furnishes designers with different approaches to store information, with every strategy having it’s points of interest and weaknesses. For this article, we’ll examine the distinctive information stockpiling methods accessible to Android engineers, alongside test code to kick you off, or to invigorate your memory.

Ways to store data.

There are basically four different ways to store data in an Android app:

1. Shared Preferences.

You should utilize this to save crude information in key-esteem sets. You have a key, which should be a String, and the relating an incentive for that key, which can be one of: boolean, skim, int, long or string. Inside, the Android stage stores an application’s Shared Preferences in a xml document in a private registry. An application can have various Shared Preferences documents. In a perfect world, you will need to utilize Shared inclinations to store application Preferences.

2. Internal Storage.

There are bunches of circumstances where you should endure information however Shared Preferences is excessively restricting. You might need to endure Java articles, or pictures. Or on the other hand your information coherently should be persevered utilizing the recognizable filesystem progression. The Internal Storage information stockpiling strategy is explicitly for those circumstances where you need to store information to the gadget filesystem, yet you don’t need some other application (even the client) to peruse this information. Information put away utilizing the Internal Storage technique is totally private to your application, and are erased from the gadget when your application is uninstalled.

3. External Storage.

Alternately, there are different occasions where you may need the client to see the records and information saved by your application, on the off chance that they wish. To save (or potentially read) records to the gadget’s outer stockpiling, your application should demand for the WRITE_EXTERNAL_STORAGE consent. On the off chance that you just need to peruse from the External Storage without composing, demand for the READ_EXTERNAL_STORAGE consent. The WRITE_EXTERNAL_STORAGE authorization awards both read/compose access. In any case, starting with Android 4.4, you can really keep in touch with a “private” outside capacity envelope without mentioning WRITE_EXTERNAL_STORAGE. The “private” envelope can be perused by different applications and by the client, nonetheless, information put away in these organizers are not examined by the media scanner. This app_private organizer is situated in the Android/information registry, and is likewise erased when your application is uninstalled.

Starting with Android 7.0, applications can demand for admittance to a specific catalog, as opposed to mentioning for admittance to the whole outer stockpiling. Thusly, your application can, for instance, demand admittance to either the photos index just, or the reports registry. This is alluded to as perused registry access. For more data about mentioning perused catalog access, look at this Android designer instructional exercise.

4. SQLite database.

At last, Android offers help for applications to utilize SQLite information bases for information stockpiling. Data sets made are application explicit, and are accessible to any class inside the application, however not to outside applications. It’s implied, that before you choose to utilize a SQLite information base for information stockpiling in your application, you ought to have some SQL information.

We’ll talk about every one of these thusly. We use information restricting strategies for our example code, and on the off chance that you are curious about this, or need a boost, look at our past article on utilizing information official in android.

Using Shared Preferences.

To store data using shared preferences, you must first get a SharedPreferences . There are two Context methods that can be used to retrieve a SharedPreferences object.

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

for when your app will have a single preferences file, and
SharedPreferences sharedPreferences = getSharedPreferences(fileNameString, MODE_PRIVATE);

for when your app could have multiple preferences files, or if you prefer to name your SharedPreferences instance.

On getting the SharedPreferences object, you then access it’s Editor using the edit() method. To actually add a value, use the Editor’s putXXX() method, where XXX is one of Boolean, String, Float, Long, Int or StringSet. You can also remove a key-value preference pair with remove().

Finally, make sure to call the Editor’s commit() method after putting or removing values. If you don’t call commit, you changes will not be persisted.

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(keyString, valueString);

For our sample app, we allow the user specify a SharedPreferences filename. If the user specifies a name, we request for the SharedPreferences having that name, if not, we request for the default SharedPreference object.

String fileNameString = sharedPreferencesBinding.fileNameEditView.getText().toString();
SharedPreferences sharedPreferences;
if(fileNameString.isEmpty()) {
    sharedPreferences = getPreferences(MODE_PRIVATE);
else {
    sharedPreferences = getSharedPreferences(fileNameString, MODE_PRIVATE);


There are focal points and disservices to utilizing every one of the diverse stockpiling strategies accessible. SharedPreferences is the most effortless to utilize, particularly in the event that you need to store discrete crude information types. Inward and outer capacity is best for putting away records, for example, music, recordings and archives, while SQLite wins on the off chance that you need to perform quick quests and inquiries on your information. The capacity technique you pick ought to eventually be subject to your information types, the period of time you need the information, and how private you need the information to be.

Of course, the total hotspot for the example application created above is accessible on github. Don’t hesitate to use as you see fit, and don’t spare a moment to connect with remarks, questions as well as tips. Upbeat coding.

Leave a Reply

Your email address will not be published. Required fields are marked *