Storage Options

Storage quickview

  • Use Shared Preferences for primitive data
  • Use internal device storage for private data

In this document

  1. Using Shared Preferences
  2. Using the Internal Storage
  3. Using a Network Connection

Mindroid provides several options for you to save persistent application data. The solution you choose depends on your specific needs.

Your data storage options are the following:

Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
Network Connection
Store data on the web with your own network server.

Using Shared Preferences

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across sessions (even if your application is killed).

To get a SharedPreferences object for your application, use the following method:

  • getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

To write values:

  1. Call edit() to get a SharedPreferences.Editor.
  2. Add values with methods such as putBoolean() and putString().
  3. Commit the new values with commit()

To read values, use SharedPreferences methods such as getBoolean() and getString().

Here is an example that saves a preference for the phone mode in a telephony application:

public class Telephony extends Service {
    public static final String PREFS_NAME = "MyPrefsFile";

    public void onCreate() {
        . . .

        // Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean phoneMode = settings.getBoolean("phoneMode", false);
        setPhoneMode(phoneMode);
    }

    public void onDestroy() {
        // We need an Editor object to make preference changes.
        // All objects are from mindroid.context.Context
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("phoneMode", mPhoneMode);

        // Commit the edits!
        editor.commit();
    }
}

Using the Internal Storage

You can save files directly on the device's internal storage.

To create and write a file to the internal storage:

  1. Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.
  2. Write to the file with write().
  3. Close the stream with close().

For example:

String FILENAME = "HelloWorld.txt";
String string = "Hello World!";

FileOutputStream fos = openFileOutput(FILENAME, 0);
fos.write(string.getBytes());
fos.close();

To read a file from internal storage:

  1. Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
  2. Read bytes from the file with read().
  3. Then close the stream with close().

Using a Network Connection

You can use the network (when it's available) to store and retrieve data on your own web-based services. To do network operations, use classes in the following packages: