Open menu with table of contents Storage Options
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Android Development

Storage Options

Stuttgart Media University

1 Agenda

  • Shared Preferences
  • Internal and External Storage
  • SQLite Database, Room & others
  • Network Connections & cloud storage

2 Shared Preferences

  • Simple way to persistently store small data entities in a key-value storage
  • Useful for data such as user preferences, app settings etc.
  • Access to storage via the class SharedPreferences
    • Obtain an instance by calling getSharedPreferences() if multiple preference files are required. First parameter is the name of the preference file
    • Or by calling getPreferences() if only one preference file is needed
  • To write values to the preferences file the class SharedPreferences.Editor can be used
    • Obtain an instance by calling edit() on the SharedPreferences instance
    • Add values using putBoolean() and putString() with the key as the first parameter and the value as the second parameter
    • Commit the new values with commit()
  • Read values using getBoolean() and getString()

3 Shared Preferences

Example code, reading values in onCreate()

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    protected void onCreate(Bundle state) {

        // Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean silent = settings.getBoolean("silentMode", false);

    // ...

4 Shared Preferences

  • Example code, writing values in onStop()
// ...

protected void onStop(){
    // We need an Editor object to make preference changes.
    // All objects are from android.context.Context
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("silentMode", mSilentMode);
    // Commit the edits!

// ...

5 Internal Storage

  • Files saved to the internal storage are private to the application
  • The files are removed when the application is uninstalled
  • Use the FileOutputStream to write data to a file
    • To obtain an instance call openFileOutput() with the filename and an operating mode:
      • MODE_PRIVATE will create or replace a file and make it private to your application
      • MODE_APPEND appends to an existing file
    • Write to the file using write()
    • Close the stream with close()

6 Internal Storage

  • Example for writing a file
String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

7 Internal Storage

  • To read a file use openInputFile() with the name of the file as parameter to retrieve a FileInputStream
  • Read bytes from the stream using read()
  • Then close the stream using close()
  • To open a static resources file, that is stored in the res/raw/ directory, use openRawResource() and pass the filename using R.raw.\<filename>

8 External Storage

  • Android devices support a shared "external storage" to save data. This is either removable SD-cards or internal (non-removable) storage.
  • External storage can be modified by the user or other apps
  • Specific directories for certain data exist, scoped directories, e.g. for Pictures, Music, etc.

9 External Storage

  • In order to read or write to the external storage, the permissions READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE must be requested and granted by the user. Do this in the Androidmanifest.xml file:
<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If both, reading and writing, is required, only the WRITE_EXTERNAL_STORAGE permission is required.

10 External Storage

Before writing to the external storage, make sure that it is available by calling getExternalStorageState():

 * Checks if external storage is available for read and write 
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    return false;

11 External Storage

  • Retrieve the path to a public directory by calling getExternalStoragePublicDirectory()
    • Pass the type of directory as parameter, e.g. DIRECTORY_MUSIC or DIRECTORY_PICTURES
public File getAlbumStorageDir(String albumName) {
    // Get the directory for the user's public pictures directory.
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    return file;

12 External Storage

  • To save app private files retrieve the path by calling getExternalFilesDir()
    • The parameter is again the type (e.g. DIRECTORY_MOVIES) or null if a specific media directory is not necessary
  • Since API Level 18 (Android 4.4) writing to app private directories does not require the WRITE_EXTERNAL_STORAGE permission
  • When the user uninstalls the app, the directory and all its content is deleted
  • The MediaStore ContentProvider does not scan private directories for media
  • Beware: files are readable by other apps through the same API

13 SQLite Databases

  • Android provides full support for SQLite databases
  • To create a new SQLite database, create a subclass of SQLiteOpenHelper and override the onCreate() method
    • In there execute a SQLite command to create tables in the database

14 SQLite Databases

public class DictionaryOpenHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DICTIONARY_TABLE_NAME = "dictionary";
    private static final String DICTIONARY_TABLE_CREATE =
                "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
                KEY_WORD + " TEXT, " +
                KEY_DEFINITION + " TEXT);";

    DictionaryOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    public void onCreate(SQLiteDatabase db) {

15 SQLite Databases

  • Get an instance of the SQLiteOpenHelper using the defined constructor
  • Write to the database by calling getWritableDatabase()
  • Read from the database by calling getReadableDatabase()
    • Both return a SQLiteDatabase object that provides methods for SQLite operations
  • Execute a SQLite query using the SQLiteDatabase query() methods
  • For complex queries use the SQLiteQueryBuilder
  • The query returns a Cursor that points to the rows found by the query
  • The Android SDK includes a sqlite3 database tool for DB debugging. You can find it on macOs under /Users/\<username>/Library/Android/sdk/platform-tools

16 Room

  • Room Library
  • Object Relational Mapping (ORM) for SQLite optimised for Android.
  • Layer on top of SQLite.
  • Local databases are usually used to cache remote data for offline usage. Therefore syncing offline and online data should be your concern, not database structure.
  • Available via Android Jetpack

17 Room gradle dependencies

Ad this dependency to your app build.gradle:

implementation ""
annotationProcessor ""

18 Entity

  • Defines database table structure via plain old java objects (POJO)
  • Entity class can be reused with other libraries like Retrofit
  • Must have a primary key
  • Is defined via @Entity annotation

19 Entity Example

Use @PrimaryKey(autoGenerate = true) to let Room create and auto increment unique ids for your entries.


public class MovieDetail {

    @PrimaryKey(autoGenerate = true)
    public int uid;

    public String Title;

    public String Year;

    public String Poster;


20 Data access object

  • Provides an interface to the database.
  • Must be implemented as interface or abstract class.
  • Room generates the DAO implementation during compiletime.
  • Good point to abstract local and remote database handling.
  • Offers methods to run native SQL queries.
  • Offers convenience methods for inserting and deleting data.
  • Can be mocked for testing purpose.
  • Integraes well with Android LifeData or RX.

21 DAO Example


import java.util.List;

public interface MovieDetailDao {

    @Query("SELECT * FROM moviedetail")
    List<MovieDetail> getAll();

    void insert(MovieDetail movieDetail);

    void delete(MovieDetail movieDetail);

22 Alternative databases

23 Database

  • Holds the link to the native Android SQLiteOpenHelper.
  • Covers the complete persistence level underneath generated code.
  • Room generates the database implementation during compile time.
  • Must be an abstract class.
  • Must include a list of used entities.
  • Database access from main thread is disabled by default. Must be specifically enabled via allowMainThreadQueries()

24 Database Example


@Database(entities = {MovieDetail.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract MovieDetailDao movieDetailDao();

25 Room Example

Insert an entity into database

new Thread(new Runnable() {
    public void run() {
        AppDatabase db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "movieDb").build();


Note: Slide very likely deprecated

26 Room Example

Get a list of entities from database

new Thread(new Runnable() {
    public void run() {
        AppDatabase db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "movieDb").build();

        List<MovieDetail> movieDetailList = db.movieDetailDao().getAll();
        Log.d("TAG", movieDetailList.get(0).Title);

Note: Slide very likely deprecated

27 Network Connections

  • To use network the standard* classes can be used
  • Android also provides classes in*
  • There are many cloud storage providers that you could use to store data on a server. Some of them also provide libraries and APIs that make life easier. Examples are:

28 Summary

  • In order to persist data on an Android device you have many options:
  • Little information such as settings can be stored using the SharedPreferences
  • Raw data such as images or other generated filed can be stored directly on the file system
  • App specific data, such as content, or user generated data can be stored in databases, such as the SQLite DB or other storages
    • ORM frameworks make life easier for the developer
  • Cloud storage is accessible via networking classes or cloud storage SDKs that do the magic for you

29 Recap Questions

  • What kind of data would you store how?
  • When is the best time to persist data?
  • What is the best way to load data from a storage?
  • Where would you place the code in an Android app?
  • In which cases is cloud storage necessary?