data-storage.jd revision a90eb8fec1c67177b614b945bcc4a4b14aaabaff
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpage.title=Data Storage
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project@jd:body
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<div id="qv-wrapper">
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<div id="qv">
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <h2>Storage quickview</h2>
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <ul>
10779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <li>Use Shared Preferences for primitive data</li>
11779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <li>Use internal device storage for private data</li>
12779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <li>Use external storage for large data sets that are not private</li>
13779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <li>Use SQLite databases for structured storage</li>
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  </ul>
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <h2>In this document</h2>
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <ol>
18779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <li><a href="#pref">Using Shared Preferences</a></li>
19a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main    <li><a href="#filesInternal">Using the Internal Storage</a>
20a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main      <ol>
21a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main        <li><a href="#InternalCache">Saving cache files</a></li>
22a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main        <li><a href="#InternalMethods">Other useful methods</a></li>
23a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main      </ol></li>
24a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main    <li><a href="#filesExternal">Using the External Storage</a>
25a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main      <ol>
26a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main        <li><a href="#MediaAvail">Checking media availability</a></li>
27a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main        <li><a href="#AccessingExtFiles">Accessing files on external storage</a></li>
28a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main        <li><a href="#SavingSharedFiles">Saving files that should be shared</a></li>
29a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main        <li><a href="#ExternalCache">Saving cache files</a></li>
30a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main      </ol></li>
31a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main    <li><a href="#db">Using Databases</a>
32a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main      <ol>
33a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main        <li><a href="#dbDebugging">Database debugging</a></li>
34a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main      </ol></li>
35779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <li><a href="#netw">Using a Network Connection</a></li>
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  </ol>
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <h2>See also</h2>
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <ol>
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    <li><a href="#pref">Content Providers and Content Resolvers</a></li>
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  </ol>
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</div>
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</div>
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
46779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Android provides several options for you to save persistent application data. The solution you
47779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainchoose depends on your specific needs, such as whether the data should be private to your
48779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainapplication or accessible to other applications (and the user) and how much space your data
49779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainrequires.
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</p>
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
52779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Your data storage options are the following:</p>
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
54779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<dl>
55779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt><a href="#pref">Shared Preferences</a></dt>
56779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Store private primitive data in key-value pairs.</dd>
57779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt><a href="#filesInternal">Internal Storage</a></dt>
58779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Store private data on the device memory.</dd>
59779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt><a href="#filesExternal">External Storage</a></dt>
60779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Store public data on the shared external storage.</dd>
61779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt><a href="#db">SQLite Databases</a></dt>
62779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Store structured data in a private database.</dd>
63779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt><a href="#netw">Network Connection</a></dt>
64779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Store data on the web with your own network server.</dd>
65779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</dl>
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
67779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Android provides a way for you to expose even your private data to other applications
68779de5c040ab53e0dba9f7a21a236209a376f548Scott Main&mdash; with a <a href="{@docRoot}guide/topics/providers/content-providers.html">content
69779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainprovider</a>. A content provider is an optional component that exposes read/write access to
70779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainyour application data, subject to whatever restrictions you want to impose. For more information
71779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainabout using content providers, see the
72779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<a href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a>
73779de5c040ab53e0dba9f7a21a236209a376f548Scott Maindocumentation.
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</p>
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
76779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
77779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
78779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
79779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h2 id="pref">Using Shared Preferences</h2>
80779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
81779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>The {@link android.content.SharedPreferences} class provides a general framework that allows you
82779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainto save and retrieve persistent key-value pairs of primitive data types. You can use {@link
83779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.SharedPreferences} to save any primitive data: booleans, floats, ints, longs, and
84779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainstrings. This data will persist across user sessions (even if your application is killed).</p>
85779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
86779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<div class="sidebox-wrapper">
87779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<div class="sidebox">
88779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h3>User Preferences</h3>
89779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Shared preferences are not strictly for saving "user preferences," such as what ringtone a
90779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainuser has chosen. If you're interested in creating user preferences for your application, see {@link
91779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.preference.PreferenceActivity}, which provides an Activity framework for you to create
92779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainuser preferences, which will be automatically persisted (using shared preferences).</p>
93779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</div>
94779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</div>
95779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
96779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>To get a {@link android.content.SharedPreferences} object for your application, use one of
97779de5c040ab53e0dba9f7a21a236209a376f548Scott Maintwo methods:</p>
98779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<ul>
99779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>{@link android.content.Context#getSharedPreferences(String,int)
100779de5c040ab53e0dba9f7a21a236209a376f548Scott MaingetSharedPreferences()} - Use this if you need multiple preferences files identified by name,
101779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainwhich you specify with the first parameter.</li>
102779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>{@link android.app.Activity#getPreferences(int) getPreferences()} - Use this if you need
103779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainonly one preferences file for your Activity. Because this will be the only preferences file
104779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfor your Activity, you don't supply a name.</li>
105779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</ul>
106779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
107779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>To write values:</p>
108779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<ol>
109779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Call {@link android.content.SharedPreferences#edit()} to get a {@link
110779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.SharedPreferences.Editor}.</li>
111779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Add values with methods such as {@link
112779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.SharedPreferences.Editor#putBoolean(String,boolean) putBoolean()} and {@link
113779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.SharedPreferences.Editor#putString(String,String) putString()}.</li>
114779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Commit the new values with {@link android.content.SharedPreferences.Editor#commit()}</li>
115779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</ol>
116779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
117779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>To read values, use {@link android.content.SharedPreferences} methods such as {@link
118779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.SharedPreferences#getBoolean(String,boolean) getBoolean()} and {@link
119779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.SharedPreferences#getString(String,String) getString()}.</p>
120779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<p>
122779de5c040ab53e0dba9f7a21a236209a376f548Scott MainHere is an example that saves a preference for silent keypress mode in a
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectcalculator:
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</p>
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<pre>
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class Calc extends Activity {
128779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    public static final String PREFS_NAME = "MyPrefsFile";
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    &#64;Override
131c4367e54e41f4e7a6c79b60dd80e66c9e6fdcfc3Scott Main    protected void onCreate(Bundle state){
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       super.onCreate(state);
133779de5c040ab53e0dba9f7a21a236209a376f548Scott Main       . . .
134779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       // Restore preferences
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       boolean silent = settings.getBoolean("silentMode", false);
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       setSilent(silent);
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
140779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    &#64;Override
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected void onStop(){
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project       super.onStop();
144779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
145779de5c040ab53e0dba9f7a21a236209a376f548Scott Main      // We need an Editor object to make preference changes.
146779de5c040ab53e0dba9f7a21a236209a376f548Scott Main      // All objects are from android.context.Context
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      SharedPreferences.Editor editor = settings.edit();
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      editor.putBoolean("silentMode", mSilentMode);
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
151779de5c040ab53e0dba9f7a21a236209a376f548Scott Main      // Commit the edits!
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      editor.commit();
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</pre>
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
160779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<a name="files"></a>
1619280c344651908feabdb9d34c5822293cf6ac256Scott Main<h2 id="filesInternal">Using the Internal Storage</h2>
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
163779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>You can save files directly on the device's internal storage. By default, files saved
164779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainto the internal storage are private to your application and other applications cannot access
165779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthem (nor can the user). When the user uninstalls your application, these files are removed.</p>
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
167779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>To create and write a private file to the internal storage:</p>
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
169779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<ol>
170779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Call {@link android.content.Context#openFileOutput(String,int) openFileOutput()} with the
171779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainname of the file and the operating mode. This returns a {@link java.io.FileOutputStream}.</li>
172779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Write to the file with {@link java.io.FileOutputStream#write(byte[]) write()}.</li>
173779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Close the stream with {@link java.io.FileOutputStream#close()}.</li>
174779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</ol>
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
176779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>For example:</p>
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
178779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<pre>
179779de5c040ab53e0dba9f7a21a236209a376f548Scott MainString FILENAME = "hello_file";
180779de5c040ab53e0dba9f7a21a236209a376f548Scott MainString string = "hello world!";
181779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
182779de5c040ab53e0dba9f7a21a236209a376f548Scott MainFileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
183779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfos.write(string.getBytes());
184779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfos.close();
185779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</pre>
186779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
187779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>{@link android.content.Context#MODE_PRIVATE} will create the file (or replace a file of
188779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthe same name) and make it private to your application. Other modes available are: {@link
189779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.Context#MODE_APPEND}, {@link
190779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.Context#MODE_WORLD_READABLE}, and {@link
191779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.Context#MODE_WORLD_WRITEABLE}.</p>
192779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
193779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>To read a file from internal storage:</p>
194779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
195779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<ol>
196779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Call {@link android.content.Context#openFileInput openFileInput()} and pass it the
197779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainname of the file to read. This returns a {@link java.io.FileInputStream}.</li>
198779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Read bytes from the file with {@link java.io.FileInputStream#read(byte[],int,int)
199779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainread()}.</li>
200779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li>Then close the stream with  {@link java.io.FileInputStream#close()}.</li>
201779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</ol>
202779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
203779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p class="note"><strong>Tip:</strong> If you want to save a static file in your application at
204779de5c040ab53e0dba9f7a21a236209a376f548Scott Maincompile time, save the file in your project <code>res/raw/</code> directory. You can open it with
205779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.content.res.Resources#openRawResource(int) openRawResource()}, passing the {@code
206779de5c040ab53e0dba9f7a21a236209a376f548Scott MainR.raw.<em>&lt;filename&gt;</em>} resource ID. This method returns an {@link java.io.InputStream}
207779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthat you can use to read the file (but you cannot write to the original file).
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</p>
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
210779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
211779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h3 id="InternalCache">Saving cache files</h3>
212779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
213779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>If you'd like to cache some data, rather than store it persistently, you should use {@link
214779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.Context#getCacheDir()} to open a {@link
215779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainjava.io.File} that represents the internal directory where your application should save
216779de5c040ab53e0dba9f7a21a236209a376f548Scott Maintemporary cache files.</p>
217779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
218779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>When the device is
219779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainlow on internal storage space, Android may delete these cache files to recover space. However, you
220779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainshould not rely on the system to clean up these files for you. You should always maintain the cache
221779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfiles yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user
222779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainuninstalls your application, these files are removed.</p>
223779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
224779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
225779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h3 id="InternalMethods">Other useful methods</h3>
226779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
227779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<dl>
228779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt>{@link android.content.Context#getFilesDir()}</dt>
229779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Gets the absolute path to the filesystem directory where your internal files are saved.</dd>
230779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt>{@link android.content.Context#getDir(String,int) getDir()}</dt>
231779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Creates (or opens an existing) directory within your internal storage space.</dd>
232779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt>{@link android.content.Context#deleteFile(String) deleteFile()}</dt>
233779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Deletes a file saved on the internal storage.</dd>
234779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <dt>{@link android.content.Context#fileList()}</dt>
235779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    <dd>Returns an array of files currently saved by your application.</dd>
236779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</dl>
237779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
238779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
239779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
240779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
2419280c344651908feabdb9d34c5822293cf6ac256Scott Main<h2 id="filesExternal">Using the External Storage</h2>
242779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
243779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Every Android-compatible device supports a shared "external storage" that you can use to
244779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainsave files. This can be a removable storage media (such as an SD card) or an internal
245779de5c040ab53e0dba9f7a21a236209a376f548Scott Main(non-removable) storage. Files saved to the external storage are world-readable and can
246779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainbe modified by the user when they enable USB mass storage to transfer files on a computer.</p>
247779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
248779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p class="caution"><strong>Caution:</strong> External files can disappear if the user mounts the
249779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainexternal storage on a computer or removes the media, and there's no security enforced upon files you
250779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainsave to the external storage. All applications can read and write files placed on the external
251779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainstorage and the user can remove them.</p>
252779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
253779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
254a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main<h3 id="MediaAvail">Checking media availability</h3>
255779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
256779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Before you do any work with the external storage, you should always call {@link
257779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.os.Environment#getExternalStorageState()} to check whether the media is available. The
258779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainmedia might be mounted to a computer, missing, read-only, or in some other state. For example,
259779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainhere's how you can check the availability:</p>
260779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
261779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<pre>
262779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainboolean mExternalStorageAvailable = false;
263779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainboolean mExternalStorageWriteable = false;
264779de5c040ab53e0dba9f7a21a236209a376f548Scott MainString state = Environment.getExternalStorageState();
265779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
266779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainif (Environment.MEDIA_MOUNTED.equals(state)) {
267779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    // We can read and write the media
268779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    mExternalStorageAvailable = mExternalStorageWriteable = true;
269779de5c040ab53e0dba9f7a21a236209a376f548Scott Main} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
270779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    // We can only read the media
271779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    mExternalStorageAvailable = true;
272779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    mExternalStorageWriteable = false;
273779de5c040ab53e0dba9f7a21a236209a376f548Scott Main} else {
274779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    // Something else is wrong. It may be one of many other states, but all we need
275779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    //  to know is we can neither read nor write
276779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    mExternalStorageAvailable = mExternalStorageWriteable = false;
277779de5c040ab53e0dba9f7a21a236209a376f548Scott Main}
278779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</pre>
279779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
280779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>This example checks whether the external storage is available to read and write. The
281779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.os.Environment#getExternalStorageState()} method returns other states that you
282779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainmight want to check, such as whether the media is being shared (connected to a computer), is missing
283779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainentirely, has been removed badly, etc. You can use these to notify the user with more information
284779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainwhen your application needs to access the media.</p>
285779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
286779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
287a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main<h3 id="AccessingExtFiles">Accessing files on external storage</h3>
288779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
289779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>If you're using API Level 8 or greater, use {@link
290779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.Context#getExternalFilesDir(String) getExternalFilesDir()} to open a {@link
291779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainjava.io.File} that represents the external storage directory where you should save your
292779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfiles. This method takes a <code>type</code> parameter that specifies the type of subdirectory you
293779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainwant, such as {@link android.os.Environment#DIRECTORY_MUSIC} and
294779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.os.Environment#DIRECTORY_RINGTONES} (pass <code>null</code> to receive
295779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthe root of your application's file directory). This method will create the
296779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainappropriate directory if necessary. By specifying the type of directory, you
297779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainensure that the Android's media scanner will properly categorize your files in the system (for
298779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainexample, ringtones are identified as ringtones and not music). If the user uninstalls your
299779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainapplication, this directory and all its contents will be deleted.</p>
300779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
301779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>If you're using API Level 7 or lower, use {@link
302779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.os.Environment#getExternalStorageDirectory()}, to open a {@link
303779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainjava.io.File} representing the root of the external storage. You should then write your data in the
304779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfollowing directory:</p>
305779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<pre class="no-pretty-print classic">
306779de5c040ab53e0dba9f7a21a236209a376f548Scott Main/Android/data/<em>&lt;package_name&gt;</em>/files/
307779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</pre>
308779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>The {@code <em>&lt;package_name&gt;</em>} is your Java-style package name, such as "{@code
309779de5c040ab53e0dba9f7a21a236209a376f548Scott Maincom.example.android.app}". If the user's device is running API Level 8 or greater and they
310779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainuninstall your application, this directory and all its contents will be deleted.</p>
311779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
312779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
313779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<div class="sidebox-wrapper" style="margin-top:3em">
314779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<div class="sidebox">
315779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
316779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h4>Hiding your files from the Media Scanner</h4>
317779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
318779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Include an empty file named {@code .nomedia} in your external files directory (note the dot
319779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainprefix in the filename). This will prevent Android's media scanner from reading your media
320779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfiles and including them in apps like Gallery or Music.</p>
321779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
322779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</div>
323779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</div>
324779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
325779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
326a90eb8fec1c67177b614b945bcc4a4b14aaabaffScott Main<h3 id="SavingSharedFiles">Saving files that should be shared</h3>
327779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
328779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>If you want to save files that are not specific to your application and that should <em>not</em>
329779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainbe deleted when your application is uninstalled, save them to one of the public directories on the
330779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainexternal storage. These directories lay at the root of the external storage, such as {@code
331779de5c040ab53e0dba9f7a21a236209a376f548Scott MainMusic/}, {@code Pictures/}, {@code Ringtones/}, and others.</p>
332779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
333779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>In API Level 8 or greater, use {@link
334779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.os.Environment#getExternalStoragePublicDirectory(String)
335779de5c040ab53e0dba9f7a21a236209a376f548Scott MaingetExternalStoragePublicDirectory()}, passing it the type of public directory you want, such as
336779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.os.Environment#DIRECTORY_MUSIC}, {@link android.os.Environment#DIRECTORY_PICTURES},
337779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.os.Environment#DIRECTORY_RINGTONES}, or others. This method will create the
338779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainappropriate directory if necessary.</p>
339779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
340779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>If you're using API Level 7 or lower, use {@link
341779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.os.Environment#getExternalStorageDirectory()} to open a {@link java.io.File} that represents
342779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthe root of the external storage, then save your shared files in one of the following
343779de5c040ab53e0dba9f7a21a236209a376f548Scott Maindirectories:</p>
344779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
345779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<ul class="nolist"></li>
346779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Music/</code> - Media scanner classifies all media found here as user music.</li>
347779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Podcasts/</code> - Media scanner classifies all media found here as a podcast.</li>
348779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Ringtones/ </code> - Media scanner classifies all media found here as a ringtone.</li>
349779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Alarms/</code> - Media scanner classifies all media found here as an alarm sound.</li>
350779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Notifications/</code> - Media scanner classifies all media found here as a notification
351779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainsound.</li>
352779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Pictures/</code> - All photos (excluding those taken with the camera).</li>
353779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Movies/</code> - All movies (excluding those taken with the camcorder).</li>
354779de5c040ab53e0dba9f7a21a236209a376f548Scott Main  <li><code>Download/</code> - Miscellaneous downloads.</li>
355779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</ul>
356779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
357779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
358779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h3 id="ExternalCache">Saving cache files</h3>
359779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
360779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>If you're using API Level 8 or greater, use {@link
361779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.content.Context#getExternalCacheDir()} to open a {@link java.io.File} that represents the
362779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainexternal storage directory where you should save cache files. If the user uninstalls your
363779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainapplication, these files will be automatically deleted. However, during the life of your
364779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainapplication, you should manage these cache files and remove those that aren't needed in order to
365779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainpreserve file space.</p>
366779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
367779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>If you're using API Level 7 or lower, use {@link
368779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.os.Environment#getExternalStorageDirectory()} to open a {@link java.io.File} that represents
369779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthe root of the external storage, then write your cache data in the following directory:</p>
370779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<pre class="no-pretty-print classic">
371779de5c040ab53e0dba9f7a21a236209a376f548Scott Main/Android/data/<em>&lt;package_name&gt;</em>/cache/
372779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</pre>
373779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>The {@code <em>&lt;package_name&gt;</em>} is your Java-style package name, such as "{@code
374779de5c040ab53e0dba9f7a21a236209a376f548Scott Maincom.example.android.app}".</p>
375779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
376779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
377779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
378779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h2 id="db">Using Databases</h2>
379779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
380779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Android provides full support for <a href="http://www.sqlite.org/">SQLite</a> databases.
381779de5c040ab53e0dba9f7a21a236209a376f548Scott MainAny databases you create will be accessible by name to any
382779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainclass in the application, but not outside the application.</p>
383779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
384779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>The recommended method to create a new SQLite database is to create a subclass of {@link
385779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.database.sqlite.SQLiteOpenHelper} and override the {@link
386779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.database.sqlite.SQLiteOpenHelper#onCreate(SQLiteDatabase) onCreate()} method, in which you
387779de5c040ab53e0dba9f7a21a236209a376f548Scott Maincan execute a SQLite command to create tables in the database. For example:</p>
388779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
389779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<pre>
390c4367e54e41f4e7a6c79b60dd80e66c9e6fdcfc3Scott Mainpublic class DictionaryOpenHelper extends SQLiteOpenHelper {
391779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
392779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    private static final int DATABASE_VERSION = 2;
393779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    private static final String DICTIONARY_TABLE_NAME = "dictionary";
394779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    private static final String DICTIONARY_TABLE_CREATE =
395779de5c040ab53e0dba9f7a21a236209a376f548Scott Main                "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +
396779de5c040ab53e0dba9f7a21a236209a376f548Scott Main                KEY_WORD + " TEXT, " +
397779de5c040ab53e0dba9f7a21a236209a376f548Scott Main                KEY_DEFINITION + " TEXT);";
398779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
399779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    DictionaryOpenHelper(Context context) {
400779de5c040ab53e0dba9f7a21a236209a376f548Scott Main        super(context, DATABASE_NAME, null, DATABASE_VERSION);
401779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    }
402779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
403779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    &#64;Override
404779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    public void onCreate(SQLiteDatabase db) {
405779de5c040ab53e0dba9f7a21a236209a376f548Scott Main        db.execSQL(DICTIONARY_TABLE_CREATE);
406779de5c040ab53e0dba9f7a21a236209a376f548Scott Main    }
407779de5c040ab53e0dba9f7a21a236209a376f548Scott Main}
408779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</pre>
409779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
410779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>You can then get an instance of your {@link android.database.sqlite.SQLiteOpenHelper}
411779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainimplementation using the constructor you've defined. To write to and read from the database, call
412779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase()} and {@link
413779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.database.sqlite.SQLiteOpenHelper#getReadableDatabase()}, respectively. These both return a
414779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.database.sqlite.SQLiteDatabase} object that represents the database and
415779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainprovides methods for SQLite operations.</p>
416779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
417779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<div class="sidebox-wrapper">
418779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<div class="sidebox">
419779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Android does not impose any limitations beyond the standard SQLite concepts. We do recommend
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectincluding an autoincrement value key field that can be used as a unique ID to
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectquickly find a record.  This is not required for private data, but if you
422779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainimplement a <a href="{@docRoot}guide/topics/providers/content-providers.html">content provider</a>,
423779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainyou must include a unique ID using the {@link android.provider.BaseColumns#_ID BaseColumns._ID}
424779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainconstant.
425779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</p>
426779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</div>
427779de5c040ab53e0dba9f7a21a236209a376f548Scott Main</div>
428779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
429779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>You can execute SQLite queries using the {@link android.database.sqlite.SQLiteDatabase}
430779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link
431779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainandroid.database.sqlite.SQLiteDatabase#query(boolean,String,String[],String,String[],String,String,String,String)
432779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainquery()} methods, which accept various query parameters, such as the table to query,
433779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthe projection, selection, columns, grouping, and others. For complex queries, such as
434779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainthose that require column aliases, you should use
435779de5c040ab53e0dba9f7a21a236209a376f548Scott Main{@link android.database.sqlite.SQLiteQueryBuilder}, which provides
436779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainseveral convienent methods for building queries.</p>
437779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
438779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>Every SQLite query will return a {@link android.database.Cursor} that points to all the rows
439779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainfound by the query. The {@link android.database.Cursor} is always the mechanism with which
440779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainyou can navigate results from a database query and read rows and columns.</p>
441779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
442779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>For sample apps that demonstrate how to use SQLite databases in Android, see the
443779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<a href="{@docRoot}resources/samples/NotePad/index.html">Note Pad</a> and
444779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<a href="{@docRoot}resources/samples/SearchableDictionary/index.html">Searchable Dictionary</a>
445779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainapplications.</p>
446779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
447779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
448779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h3 id="dbDebugging">Database debugging</h3>
449779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
450779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>The Android SDK includes a {@code sqlite3} database tool that allows you to browse
451779de5c040ab53e0dba9f7a21a236209a376f548Scott Maintable contents, run SQL commands, and perform other useful functions on SQLite
452779de5c040ab53e0dba9f7a21a236209a376f548Scott Maindatabases.  See <a href="{@docRoot}guide/developing/tools/adb.html#sqlite">Examining sqlite3
453779de5c040ab53e0dba9f7a21a236209a376f548Scott Maindatabases from a remote shell</a> to learn how to run this tool.
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</p>
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
457779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
458779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
459779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
460779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<h2 id="netw">Using a Network Connection</h2>
461779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
462779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<!-- TODO MAKE THIS USEFUL!! -->
463779de5c040ab53e0dba9f7a21a236209a376f548Scott Main
464779de5c040ab53e0dba9f7a21a236209a376f548Scott Main<p>You can use the network (when it's available) to store and retrieve data on your own web-based
465779de5c040ab53e0dba9f7a21a236209a376f548Scott Mainservices. To do network operations, use classes in the following packages:</p>
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project<ul class="no-style">
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <li><code>{@link java.net java.net.*}</code></li>
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project  <li><code>{@link android.net android.net.*}</code></li>
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</ul>
471