Over a million developers have joined DZone.

Data Saving Practices for Android App Devs

DZone's Guide to

Data Saving Practices for Android App Devs

Let's have a closer look at all of the data storage aspects used by various app developers and how they store various data formats during android application development.

· Mobile Zone ·
Free Resource

Every business application needs to store data in one or another. Though the format of data differs, data storage for every application is like taking a breath to survive. Data can be in numerous formats such as user settings, images, videos, app settings, or anything which is either extracted from the internet or is application generated.

Due to the increasing usage of applications amongst folks in the current scenario, numerous types of data are generated that belong to the user. Such types of data need to be stored in the public cloud so that it can be accessed by the user. But regardless of this, there are many other applications on the market that want to store data in a way that it cannot be accessed by the user or any third party application. For such a scenario, the android platform leverages developers with multiple ways for data storage, each method having its own benefits and limitations. Let's have a closer look at all of the data storage aspects used by various app developers and how they store various data formats during android application development.

Data Storage Ways for Developer

Method 1: Using Shared Preference

When Can It Be Used?

Generally, when you have small sets of data like password, ID, or any kind of primitive data which you need to refer at a later stage when the user launches the app in such case, shared preference concept can be used. Here all your data is stored in a key — value pair. Shared preference can be used to store any data type like boolean, float, int, string, etc. Whenever any kind of data is stored in shared preference then, it will remain constant or you can say persistent across the sessions.

How to Create Shared Preference?

If you want to store data in shared preference then, you need to create its object first.

SharedPreferences sp = getPreferences( MODE_PRIVATE);

This method is used when you have single file preferences. For multiple preferences,

SharedPreferences sp = getsharedPreferences( fileNameString,MODE_PRIVATE);

Here, you need to mention your shared preference instance name.

Method 2: Internal Storage

When Can It Be Used?

There are situations when you want your data to remain persistent but, shared preference has its limitations. There may be a scenario where instead of storing data in a key-value pair, you need to store data in form of images, java objects etc which should not be accessed. In such case, internal storage is used which is exclusively designed to store data in device filesystem that can be accessed by the user or any other application. Data, when stored in the internal storage is private to your application and is wiped up when you uninstall the app from your smartphone device.

How to Store Data in Internal Storage?

Internal storage is same as that of any other file system. File object reference can be obtained and data can be stored using FileOutputStream. The unique keynote of internal storage is that data stored can be accessed by your application only.


File directory;
if (filename.isEmpty()) {
    directory = getFilesDir();
else {
    directory = getDir(filename, MODE_PRIVATE);
File[] files = directory.listFiles();

Here, getFilesDir() is used to access your internal file directory. GetDir() method is used to point out file object corresponding to specified directory. So, if your filename is empty then, it will create it else will return the reference file object.

Method 3: External Storage

When you want users to view files and the data saved by your application then, external storage is used. Almost every android device do support shared external device where you can save your data. This external storage is in the form of SD card or internal storage. Files that are saved in external memory can be read and modified by the user. If a user wants to read a file from the external device then, WRITE_EXTERNAL_STORAGE permission is requested by the user. If users want to read from external storage then, READ_EXTERNAL_STORAGE permission needs to be requested by the user.

Example: Here it checks that whether internal storage is available to read and write or not.

public boolean isExternalStorageWritable() {
  String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    return false;

Method 4: SQLite Database

One of the best methods to secure and store any application data is storage in the database. Android provides SQLite database to store application data. Database created is application specific and is available to any class inside application but, cannot be accessed outside of the app.

Example: Here we create a table using SQLite database.

 public class SampleSQLiteDBHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 2;
    public static final String DATABASE_NAME = "sample_database";
    public static final String PERSON_TABLE_NAME = "person";
    public static final String PERSON_COLUMN_ID = "_id";
    public static final String PERSON_COLUMN_NAME = "name";
    public static final String PERSON_COLUMN_AGE = "age";
    public static final String PERSON_COLUMN_GENDER = "gender";

    public SampleSQLiteDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE " + PERSON_TABLE_NAME + " (" +
                PERSON_COLUMN_NAME + " TEXT, " +
                PERSON_COLUMN_AGE + " INT UNSIGNED, " +
                PERSON_COLUMN_GENDER + " TEXT" + ")");

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + PERSON_TABLE_NAME);

If you want to read and write data from DB then use getWritableDatabase() & getReadableDatabase() methods. It will return you database object that represents your database and will give access to methods that will help you in performing operations.

Saving Cache Files

Android provides a way to cache data, rather than storing it permanently in the storage system. Data can be cached either in external or internal storage. Android may delete the cache files when the device is running with low space.

Final Thoughts

Each and every storage approaches have their own advantages and limitations. Depending upon your data type and data mode, you need to select appropriate data storage approaches. Feel free to ask and don't hesitate to touch base via comments or queries. Stay calm and Happy Coding!

andoid ,android app development ,android app developers ,app developers

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}