blob: 2b3491ccf1a0788a6e298f1cd2f88511df436545 [file] [log] [blame]
page.title=Data Storage
@jd:body
<p>
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.
<p>
However, Android also provides a standard way for an application to expose
its private data to other applications &mdash; through a 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,
<a href="{@docRoot}devel/data/contentproviders.html">Content Providers</a>.
<p>
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:
<dl>
<dd><a href="#pref">Preferences</a>
<dd><a href="#files">Files</a>
<dd><a href="#db">Databases</a>
<dd><a href="#netw">Network</a>
</dl>
<dl>
<dt><a name="pref"></a><b>Preferences</b></dt>
<dd>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
<code>{@link android.content.Context#getSharedPreferences(java.lang.String,int)
Context.getSharedPreferences()}</code> 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 <code>{@link android.app.Activity#getPreferences(int)
Activity.getPreferences()}</code> with no name to keep them private to the calling
activity. You cannot share preferences across applications (except by using a
content provider).
<p>
Here is an example of setting user preferences for silent keypress mode for a
calculator:
<pre>
import android.app.Activity;
import android.content.SharedPreferences;
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
. . .
&#64;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);
}
&#64;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();
}
}
</pre></dd>
<p>
<dt><a name="files"></a><b>Files</b></dt>
<dd>You can store files directly on the mobile device or on a removable
storage medium. By default, other applications cannot access these files.
<p>
To read data from a file, call {@link android.content.Context#openFileInput
Context.openFileInput()} and pass it the local name and path of the file.
It returns a standard Java {@link java.io.FileInputStream} object. To write
to a file, call {@link android.content.Context#openFileOutput
Context.openFileOutput()} with the name and path. It returns a {@link
java.io.FileOutputStream} object. Calling these methods with name and path
strings from another application will not work; you can only access local
files.
<p>
If you have a static file to package with your application at compile time,
you can save the file in your project in <code>res/raw/<em>myDataFile</em></code>,
and then open it with {@link
android.content.res.Resources#openRawResource(int) Resources.openRawResource
(R.raw.<em>myDataFile</em>)}. It returns an {@link java.io.InputStream}
object that you can use to read from the file.
</dd>
<dt><a name="db"></a><b>Databases</b></dt>
<dd>The Android API contains support for creating and using SQLite databases.
Each database is private to the application that creates it.
<p>
The {@link android.database.sqlite.SQLiteDatabase} object represents a database
and has methods for interacting with it &mdash; making queries and managing the
data. To create the database, call <code>{@link
android.database.sqlite.SQLiteDatabase#create SQLiteDatabase.create()}</code>
and also subclass {@link android.database.sqlite.SQLiteOpenHelper}.
<p>
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.
<p>
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 <a href="{@docRoot}reference/adb.html#sqlite">Examine databases
(sqlite3)</a> to learn how to run this program.
<p>
All databases, SQLite and others, are stored on the device in
<code>/data/data/<em>package_name</em>/databases</code>.
<p>
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
<a href="{@docRoot}devel/data/contentproviders.html">Content Providers</a>
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.</p>
</dd>
<dt><a name="netw"></a><b>Network</b></dt>
<dd>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:
<dl>
<dd><code>{@link java.net java.net.*}</code></dd>
<dd><code>{@link android.net android.net.*}</code></dd>
</dl>
</dd>
</dl>