• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Data Storage
2@jd:body
3
4
5<div id="qv-wrapper">
6<div id="qv">
7
8  <h2>Storage quickview</h2>
9  <ul>
10    <li>Fast, lightweight storage through system preferences</li>
11    <li>File storage to device internal or removable flash</li>
12    <li>Arbitrary and structured storage in databases</li>
13    <li>Support for network-based storage</li>
14  </ul>
15
16  <h2>In this document</h2>
17  <ol>
18    <li><a href="#pref">Preferences</a></li>
19    <li><a href="#files">Files</a></li>
20    <li><a href="#db">Databases</a></li>
21    <li><a href="#netw">Network</a></li>
22  </ol>
23
24  <h2>See also</h2>
25  <ol>
26    <li><a href="#pref">Content Providers and Content Resolvers</a></li>
27  </ol>
28
29</div>
30</div>
31
32<p>
33A typical desktop operating system provides a common file system that any
34application can use to store files that can be read by other
35applications (perhaps with some access control settings).  Android uses a
36different system:  On Android, all application data (including files) are
37private to that application.
38</p>
39
40<p>
41However, Android also provides a standard way for an application to expose
42its private data to other applications &mdash; through content providers.
43A content provider is an
44optional component of an application that exposes read/write access to the
45application's data, subject to whatever restrictions it might impose.
46Content providers implement a standard syntax for requesting and modifying
47data, and a standard mechanism for reading the returned data.  Android supplies
48a number of content providers for standard data types, such as image, audio,
49and video files and personal contact information.  For more information on
50using content providers, see a separate document,
51<a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>.
52</p>
53
54<p>
55Whether or not you want to export your application's data to others,
56you need a way to store it.  Android provides the following four mechanisms
57for storing and retrieving data: <a href="#pref">Preferences</a>,
58<a href="#files">Files</a>, <a href="#db">Databases</a>, and <a href="#netw">Network</a>.
59</p>
60
61
62<h2 id="pref">Preferences</h2>
63<p>Preferences is a lightweight mechanism to store and retrieve key-value pairs of primitive
64data types.  It is typically used to store application preferences, such as a
65default greeting or a text font to be loaded whenever the application is started. Call
66<code>{@link android.content.Context#getSharedPreferences(java.lang.String,int)
67Context.getSharedPreferences()}</code> to read and write values.  Assign a name to
68your set of preferences if you want to share them with other components in the same
69application, or use <code>{@link android.app.Activity#getPreferences(int)
70Activity.getPreferences()}</code> with no name to keep them private to the calling
71activity.  You cannot share preferences across applications (except by using a
72content provider).
73</p>
74
75<p>
76Here is an example of setting user preferences for silent keypress mode for a
77calculator:
78</p>
79
80<pre>
81import android.app.Activity;
82import android.content.SharedPreferences;
83
84public class Calc extends Activity {
85public static final String PREFS_NAME = "MyPrefsFile";
86    . . .
87
88    &#64;Override
89    protected void onCreate(Bundle state){
90       super.onCreate(state);
91
92    . . .
93
94       // Restore preferences
95       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
96       boolean silent = settings.getBoolean("silentMode", false);
97       setSilent(silent);
98    }
99
100    &#64;Override
101    protected void onStop(){
102       super.onStop();
103
104      // Save user preferences. We need an Editor object to
105      // make changes. All objects are from android.context.Context
106      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
107      SharedPreferences.Editor editor = settings.edit();
108      editor.putBoolean("silentMode", mSilentMode);
109
110      // Don't forget to commit your edits!!!
111      editor.commit();
112    }
113}
114</pre>
115
116
117<h2 id="files">Files</h2>
118<p>You can store files directly on the mobile device or on a removable
119storage medium.  By default, other applications cannot access these files.
120</p>
121
122<p>
123To read data from a file, call {@link android.content.Context#openFileInput
124Context.openFileInput()} and pass it the local name and path of the file.
125It returns a standard Java {@link java.io.FileInputStream} object.  To write
126to a file, call {@link android.content.Context#openFileOutput
127Context.openFileOutput()} with the name and path.  It returns a {@link
128java.io.FileOutputStream} object.  Calling these methods with name and path
129strings from another application will not work; you can only access local
130files.
131</p>
132
133<p>
134If you have a static file to package with your application at compile time,
135you can save the file in your project in <code>res/raw/<em>myDataFile</em></code>,
136and then open it with {@link
137android.content.res.Resources#openRawResource(int) Resources.openRawResource
138(R.raw.<em>myDataFile</em>)}.  It returns an {@link java.io.InputStream}
139object that you can use to read from the file.
140</p>
141
142<h2 id="db">Databases</h2>
143<p>The Android API contains support for creating and using SQLite databases.
144Each database is private to the application that creates it.
145</p>
146
147<p>
148The {@link android.database.sqlite.SQLiteDatabase} object represents a database
149and has methods for interacting with it &mdash; making queries and managing the
150data.  To create the database, call <code>{@link
151android.database.sqlite.SQLiteDatabase#create SQLiteDatabase.create()}</code>
152and also subclass {@link android.database.sqlite.SQLiteOpenHelper}.
153</p>
154
155<p>
156As part of its support for the SQLite database system, Android exposes
157database management functions that let you store complex collections of data
158wrapped into useful objects.  For example, Android defines a data type
159for contact information; it consists of many fields including a first and last
160name (strings), an address and phone numbers (also strings), a photo (bitmap
161image), and much other information describing a person.
162</p>
163
164<p>
165Android ships with the sqlite3 database tool, which enables you to browse
166table contents, run SQL commands, and perform other useful functions on SQLite
167databases.  See <a href="{@docRoot}guide/developing/tools/adb.html#sqlite">Examine databases
168(sqlite3)</a> to learn how to run this program.
169</p>
170
171<p>
172All databases, SQLite and others, are stored on the device in
173<code>/data/data/<em>package_name</em>/databases</code>.
174</p>
175
176<p>
177Discussion of how many tables to create, what fields they contain, and how
178they are linked, is beyond the scope of this note, but Android does not
179impose any limitations beyond the standard SQLite concepts.  We do recommend
180including an autoincrement value key field that can be used as a unique ID to
181quickly find a record.  This is not required for private data, but if you
182implement a content provider, you must include such a unique ID field.  See the
183<a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
184document for more information on this field and the NotePadProvider class
185in the NotePad sample code for an example of creating and populating a
186new database.  Any databases you create will be accessible by name to any other
187class in the application, but not outside the application.
188</p>
189
190
191<h2 id="netw">Network</h2>
192<p>You can also use the network to store and retrieve data (when it's available).
193To do network operations, use the classes in the following packages:</p>
194
195<ul class="no-style">
196  <li><code>{@link java.net java.net.*}</code></li>
197  <li><code>{@link android.net android.net.*}</code></li>
198</ul>
199
200