Register for Prevent bottlenecks during mobile development on September 13
Register
White arrow point right
All

Where Does Content Get Stored With Mobile App Development? A Beginner's Guide

Pius Aboyi
Pius Aboyi
Where Does Content Get Stored With Mobile App Development? A Beginner's Guide
September 6, 2022
8
min read

Mobile apps are everywhere today, and you can find one to solve just about any problem. Some of these apps generate data that need storage.

Storing content in mobile apps has several advantages. For example, you may store data in your mobile application for offline support so that users can access a cached version of your content without an internet connection. Both iOS and Android offer multiple options for storing content. Therefore, it's important that you're familiar with each option so you can choose the best one for your use case.

In this post, you'll learn about various ways you can store content in your mobile app. We'll also walk you through some examples.

Types of Mobile App Storage

As mentioned earlier, there are different types of mobile app storage services.

1. Local Storage

Local storage refers to different services and features that make it possible to store data offline on a device. This type of storage is ideal for storing content that users generate while using your application. For example, if you're developing a to-do app, you may store each to-do item (task) using a local storage service.

You can implement local storage on your application using a file system, SharedPreferences, or a local database like SQLite.

2. Internal Storage

Modern smartphones have some dedicated memory components for internal storage. A major advantage of internal storage is that, unlike external storage, it's always available. That's to say, users can not unplug it like an external SD card or flash drive. And as a result, it's very reliable.

Because of the reliability of internal storage, it's ideal for storing data that your app depends on to function correctly or start up. However, internal storage may have a small, fixed size, hence you should only store content that's necessary. In addition, apps can only read or write data to their own internal space. That's to say, your application cannot read data stored by another application to the internal storage.

Internal storage services include shared preferences, SQLite, and file system caching.

3. External Storage

External storage includes media like a physical SD card, flash drive, and a mounted virtual SD card. In Android, external storage is referred to as "sdcard."

External storage typically offers a shared file system. This means your application may read and write to files and directories created by other applications. Similarly, other applications can also read or write the content your application stores to the external storage.

The ideal use case for external storage is persisting data to disk, especially large multimedia files like photos and videos that users may access from another application.

4. Scoped Storage

This type of storage adds scoped access to external storage so an app can only access an app-specific directory and specific media types.

Mobile Data Storage Services and Methods

Mobile apps use different services to implement local, internal, and external storage. Before we continue, let's discuss some of these services.

SharedPreferences

A common storage feature in Android is SharedPreferences. It stores data as a key-value pair and is ideal for a small amount of data. Also, by default, you can only store primitive data types using SharedPreferences. The SharedPreferences class provides methods for reading and writing data.

As the name says, you should use this type of storage for saving user preferences (settings) for your application.

Mobile apps use different services to implement local, internal, and external storage.

UserDefaults

UserDefaults is also a key-value pair data storage system. We may call UserDefaults the alternative for SharedPreferences in iOS.

SQLite

SQLite is a lightweight local SQL database. It stores data in tables that have columns to describe each field. A single entry of data in an SQLite table is a row.

SQLite can store large, related data in tables. For example, your app may have a weather readings table where you store temperatures for multiple cities and days in rows.

In Android, you can read and write data to SQLite using the Room dependency.

Core Data

Core Data is an object-relational mapping (ORM) framework for iOS. Under the hood, Core Data creates an SQLite database that is stored on the device.

File System

A file system persists data to disk as a file. Files can be in different formats, ranging from multimedia files like pictures, videos, and audio files to custom file types specific to your application.

The file system is usually the best option for storing data that's in the form of large files. However, what you can store using the file system is not limited. In Android, you can store data on both internal and external file systems, depending on the needs of your app.

Determining the Right Storage for Your Mobile Application (When to Use Each Storage System)

The table below shows each type of storage system and its best use case.

Storage SystemWhen to UseAccess TypeInternal  SharedPreferencesUse when you need to store small data in key-value pairs format, e.g., saving user preferences and custom settingsPrivate to appInternal StorageIdeal for when you want to store files that other apps should not accessPrivate to appSQLiteThis is perfect for storing multiple relational data that may require reading with using an index and advance queriesPrivate to appExternal   External Storage (sdcard)Good for storing large media files and files that other applications read and writeShared access across multiple appsScoped StorageGood for storing large data and files that you want app-specific access to on the sdcardPrivate to app

How Mobile Applications Store and Retrieve Data

Mobile apps store and retrieve data using APIs available in the mobile development framework or custom APIs by third-party frameworks. These APIs expose methods you can use to set up features that read and write data. For instance, in SharedPreferences, you can store a string using the putString() method. The complete syntax for this method is as follows:


putString("name", "Pius Aboyi")

Still, within the SharedPreferences API, there are other methods for writing data, like putInt(), and putLong() for integer and long data types respectively.

To further explain how the APIs work, let's walk through some examples of how to store and read data in an Android app.

Mobile apps store and retrieve data using APIs available in the mobile development framework or custom APIs by third-party frameworks.

Example 1: Store Data Using Shared Preference

We already know that SharedPreferences is ideal for saving user settings, so let's see how we can store settings for the default theme for an application. Our example app here has two themes, dark mode and light mode.

First, we need to implement a settings activity from which users can set their theme preferences. The following code will initialize SharedPrefences and save the user's choice with the toggle of a button:


val sharedPref: SharedPreferences = getSharedPreferences("name", Context.MODE_PRIVATE)
val darkModeEnabled = sharedPref.getBoolean("theme", false)

val editor = sharedPref.edit()
var newTheme: Boolean
newTheme = !darkModeEnabled

editor.putBoolean("theme", newTheme)
editor.apply()

From the above code, we're also reading data from SharedPreferences using the getBoolean() method. This method returns the current value for the "theme" preference key. After that, we set the value for newTheme to the inverse of the current value. That is, true becomes false, then we write the new value to SharedPreferences using the putBoolean() method. Finally, we save all changes by calling the apply() method.

Example 2: Accessing Android Internal Storage Programmatically

In this example, we'll save a text file to internal storage. This file will contain a simple text saying, "Hello world!" Then we'll attempt to read the file and display the text inside the app.

In order to follow along better, create an app with an activity that has three buttons and a TextView. One button will be Save, another Read, and the third Clear.

Next, inside your activity class add the following three methods:

Write data


fun writeData(message: String) {
    try {
        val fileOutputStream: FileOutputStream = openFileOutput("hello.txt", Context.MODE_PRIVATE)
        val outputStreamWriter = OutputStreamWriter(fileOutputStream)
        outputStreamWriter.write(message)
        outputStreamWriter.close()
    } catch (e: Exception) {
        Log.i("FILE_WRITE", e.message.toString())
    }
}

Clear data


fun clear() {
    writeData("")
}

Read data


fun readData(): String {
    var message = ""
    try {
        val fileInputStream: FileInputStream = openFileInput("hello.txt")
        val inputStreamReader: InputStreamReader = InputStreamReader(fileInputStream)

        message = inputStreamReader.readText()

    } catch (e: Exception) {
        Log.i("FILE_Read", e.message.toString())
    }

    return message
}

Once you have all the methods, call them off the main UI thread in the onClick call back for the corresponding buttons.

The hello.txt file will be stored privately in your application in the internal storage. You can find the file inside the Android/data/[your package name] directory.

Conclusion

In this post, we discussed storage in mobile applications. Mobile apps store content in multiple locations using different methods. Some of the methods we covered include SQLite, SharedPreferences, UserDefaults, and file systems.

Some storage methods are internal, access to data using internal storage is usually app-specific, and private and data is deleted once your app is uninstalled. External storage, on the other hand, allows other apps to read and write data generated from your application. Data in external storage remains even after a user deletes your app.

You can find the complete code for the example app in this Github repo.

This post was written by Pius Aboyi. Pius is a mobile and web developer with over 4 years of experience building for the Android platform. He writes code in Java, Kotlin, and PHP. He loves writing about tech and creating how-to tutorials for developers.

Automate testing for your mobile app.Start building your first test for free.

Subscribe to our newsletter for the latest news and resources

Thank you for subscribing to our blog!