A typical desktop operating system provides a common file system that any application can use to store files that can be read by other applications (perhaps with some access control settings). Android uses a different system: On Android, all application data (including files) are private to that application.
However, Android also provides a standard way for an application to expose its private data to other applications — through content providers. A content provider is an optional component of an application that exposes read/write access to the application's data, subject to whatever restrictions it might impose. Content providers implement a standard syntax for requesting and modifying data, and a standard mechanism for reading the returned data. Android supplies a number of content providers for standard data types, such as image, audio, and video files and personal contact information. For more information on using content providers, see a separate document, Content Providers.
Whether or not you want to export your application's data to others, you need a way to store it. Android provides the following four mechanisms for storing and retrieving data: Preferences, Files, Databases, and Network.
Preferences is a lightweight mechanism to store and retrieve key-value pairs of primitive
data types. It is typically used to store application preferences, such as a
default greeting or a text font to be loaded whenever the application is started. Call
Context.getSharedPreferences()
to read and write values. Assign a name to
your set of preferences if you want to share them with other components in the same
application, or use Activity.getPreferences()
with no name to keep them private to the calling
activity. You cannot share preferences across applications (except by using a
content provider).
Here is an example of setting user preferences for silent keypress mode for a calculator:
import android.app.Activity; import android.content.SharedPreferences; public class Calc extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; . . . @Override protected void onCreate(Bundle state){ super.onCreate(state); . . . // Restore preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } @Override protected void onStop(){ super.onStop(); // Save user preferences. We need an Editor object to // make changes. All objects are from android.context.Context SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Don't forget to commit your edits!!! editor.commit(); } }
You can store files directly on the mobile device or on a removable storage medium. By default, other applications cannot access these files.
To read data from a file, call Context.openFileInput() and pass it the local name and path of the file. It returns a standard Java FileInputStream object. To write to a file, call Context.openFileOutput() with the name and path. It returns a FileOutputStream object. Calling these methods with name and path strings from another application will not work; you can only access local files.
If you have a static file to package with your application at compile time,
you can save the file in your project in res/raw/myDataFile
,
and then open it with Resources.openRawResource
(R.raw.myDataFile). It returns an InputStream
object that you can use to read from the file.
The Android API contains support for creating and using SQLite databases. Each database is private to the application that creates it.
The SQLiteDatabase object represents a database
and has methods for interacting with it — making queries and managing the
data. To create the database, call SQLiteDatabase.create()
and also subclass SQLiteOpenHelper.
As part of its support for the SQLite database system, Android exposes database management functions that let you store complex collections of data wrapped into useful objects. For example, Android defines a data type for contact information; it consists of many fields including a first and last name (strings), an address and phone numbers (also strings), a photo (bitmap image), and much other information describing a person.
Android ships with the sqlite3 database tool, which enables you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases. See Examine databases (sqlite3) to learn how to run this program.
All databases, SQLite and others, are stored on the device in
/data/data/package_name/databases
.
Discussion of how many tables to create, what fields they contain, and how they are linked, is beyond the scope of this note, but Android does not impose any limitations beyond the standard SQLite concepts. We do recommend including an autoincrement value key field that can be used as a unique ID to quickly find a record. This is not required for private data, but if you implement a content provider, you must include such a unique ID field. See the Content Providers document for more information on this field and the NotePadProvider class in the NotePad sample code for an example of creating and populating a new database. Any databases you create will be accessible by name to any other class in the application, but not outside the application.
You can also use the network to store and retrieve data (when it's available). To do network operations, use the classes in the following packages: