Android - Internal Storage Tutorial

<p >Android provides many kinds of storage for applications to store their data. These storage places are shared preferences, internal and external storage, SQLite storage, and storage via network connection.</p> <p >In this chapter we are going to look at the internal storage. Internal storage is the storage of the private data on the device memory.</p> <p >By default these files are private and are accessed by only your application and get deleted , when user delete your application.</p> <h2 >Writing file</h2> <p >In order to use internal storage to write some data in the file, call the openFileOutput() method with the name of the file and the mode. The mode could be private , public e.t.c. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="typ" >FileOutputStream</span><span class="pln" > fOut </span><span class="pun" >=</span><span class="pln" > openFileOutput</span><span class="pun" >(</span><span class="str" >&quot;file name here&quot;</span><span class="pun" >,</span><span class="pln" >MODE_WORLD_READABLE</span><span class="pun" >);</span></pre> <p >The method openFileOutput() returns an instance of FileOutputStream. So you receive it in the object of FileInputStream. After that you can call write method to write data on the file. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="typ" >String</span><span class="pln" > str </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >&quot;data&quot;</span><span class="pun" >;</span><span class="pln" > fOut</span><span class="pun" >.</span><span class="pln" >write</span><span class="pun" >(</span><span class="pln" >str</span><span class="pun" >.</span><span class="pln" >getBytes</span><span class="pun" >());</span><span class="pln" > fOut</span><span class="pun" >.</span><span class="pln" >close</span><span class="pun" >();</span></pre> <h2 >Reading file</h2> <p >In order to read from the file you just created , call the openFileInput() method with the name of the file. It returns an instance of FileInputStream. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="typ" >FileInputStream</span><span class="pln" > fin </span><span class="pun" >=</span><span class="pln" > openFileInput</span><span class="pun" >(</span><span class="pln" >file</span><span class="pun" >);</span></pre> <p >After that, you can call read method to read one character at a time from the file and then you can print it. Its syntax is given below &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="kwd" >int</span><span class="pln" > c</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > temp</span><span class="pun" >=</span><span class="str" >&quot;&quot;</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >while</span><span class="pun" >(</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >c </span><span class="pun" >=</span><span class="pln" > fin</span><span class="pun" >.</span><span class="pln" >read</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >!=</span><span class="pln" > </span><span class="pun" >-</span><span class="lit" >1</span><span class="pun" >){</span><span class="pln" > temp </span><span class="pun" >=</span><span class="pln" > temp </span><span class="pun" >+</span><span class="pln" > </span><span class="typ" >Character</span><span class="pun" >.</span><span class="pln" >toString</span><span class="pun" >((</span><span class="kwd" >char</span><span class="pun" >)</span><span class="pln" >c</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="com" >//string temp contains all the data of the file.</span><span class="pln" > fin</span><span class="pun" >.</span><span class="pln" >close</span><span class="pun" >();</span></pre> <p >Apart from the the methods of write and close, there are other methods provided by the<b >FileOutputStream</b>class for better writing files. These methods are listed below &minus;</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No</th> <th >Method &amp; description</th> </tr> <tr > <td >1</td> <td ><b >FileOutputStream(File file, boolean append)</b> <p >This method constructs a new FileOutputStream that writes to file.</p> </td> </tr> <tr > <td >2</td> <td ><b >getChannel()</b> <p >This method returns a write-only FileChannel that shares its position with this stream</p> </td> </tr> <tr > <td >3</td> <td ><b >getFD()</b> <p >This method returns the underlying file descriptor</p> </td> </tr> <tr > <td >4</td> <td ><b >write(byte[] buffer, int byteOffset, int byteCount)</b> <p >This method Writes count bytes from the byte array buffer starting at position offset to this stream</p> </td> </tr> </tbody> </table> <p >Apart from the the methods of read and close, there are other methods provided by the<b >FileInputStream</b>class for better reading files. These methods are listed below &minus;</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No</th> <th >Method &amp; description</th> </tr> <tr > <td >1</td> <td ><b >available()</b> <p >This method returns an estimated number of bytes that can be read or skipped without blocking for more input</p> </td> </tr> <tr > <td >2</td> <td ><b >getChannel()</b> <p >This method returns a read-only FileChannel that shares its position with this stream</p> </td> </tr> <tr > <td >3</td> <td ><b >getFD()</b> <p >This method returns the underlying file descriptor</p> </td> </tr> <tr > <td >4</td> <td ><b >read(byte[] buffer, int byteOffset, int byteCount)</b> <p >This method reads at most length bytes from this stream and stores them in the byte array b starting at offset</p> </td> </tr> </tbody> </table> <h2 >Example</h2> <p >Here is an example demonstrating the use of internal storage to store and read files. It creates a basic storage application that allows you to read and write from internal storage.</p> <p >To experiment with this example, you can run this on an actual device or in an emulator.</p> <table class="table table-bordered" > <tbody > <tr > <th >Steps</th> <th >Description</th> </tr> <tr > <td >1</td> <td >You will use Android Studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.</td> </tr> <tr > <td >2</td> <td >Modify src/MainActivity.java file to add necessary code.</td> </tr> <tr > <td >3</td> <td >Modify the res/layout/activity_main to add respective XML components</td> </tr> <tr > <td >4</td> <td >Run the application and choose a running android device and install the application on it and verify the results</td> </tr> </tbody> </table> <p >Following is the content of the modified main activity file<b >src/MainActivity.java</b>.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="pln" >sairamkrishna</span><span class="pun" >.</span><span class="pln" >myapplication</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >Activity</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >Bundle</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >Menu</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >MenuItem</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >View</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Button</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >EditText</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >TextView</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >Toast</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >io</span><span class="pun" >.</span><span class="typ" >FileInputStream</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > java</span><span class="pun" >.</span><span class="pln" >io</span><span class="pun" >.</span><span class="typ" >FileOutputStream</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >Activity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >Button</span><span class="pln" > b1</span><span class="pun" >,</span><span class="pln" >b2</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >TextView</span><span class="pln" > tv</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >EditText</span><span class="pln" > ed1</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > data</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >private</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > file </span><span class="pun" >=</span><span class="pln" > </span><span class="str" >&quot;mydata&quot;</span><span class="pun" >;</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" > setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" > b1</span><span class="pun" >=(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >button</span><span class="pun" >);</span><span class="pln" > b2</span><span class="pun" >=(</span><span class="typ" >Button</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >button2</span><span class="pun" >);</span><span class="pln" > ed1</span><span class="pun" >=(</span><span class="typ" >EditText</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >editText</span><span class="pun" >);</span><span class="pln" > tv</span><span class="pun" >=(</span><span class="typ" >TextView</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >textView2</span><span class="pun" >);</span><span class="pln" > b1</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > data</span><span class="pun" >=</span><span class="pln" >ed1</span><span class="pun" >.</span><span class="pln" >getText</span><span class="pun" >().</span><span class="pln" >toString</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >try</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >FileOutputStream</span><span class="pln" > fOut </span><span class="pun" >=</span><span class="pln" > openFileOutput</span><span class="pun" >(</span><span class="pln" >file</span><span class="pun" >,</span><span class="pln" >MODE_WORLD_READABLE</span><span class="pun" >);</span><span class="pln" > fOut</span><span class="pun" >.</span><span class="pln" >write</span><span class="pun" >(</span><span class="pln" >data</span><span class="pun" >.</span><span class="pln" >getBytes</span><span class="pun" >());</span><span class="pln" > fOut</span><span class="pun" >.</span><span class="pln" >close</span><span class="pun" >();</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="pln" >getBaseContext</span><span class="pun" >(),</span><span class="str" >&quot;file saved&quot;</span><span class="pun" >,</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_SHORT</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >catch</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >Exception</span><span class="pln" > e</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// TODO Auto-generated catch block</span><span class="pln" > e</span><span class="pun" >.</span><span class="pln" >printStackTrace</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > b2</span><span class="pun" >.</span><span class="pln" >setOnClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >try</span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >FileInputStream</span><span class="pln" > fin </span><span class="pun" >=</span><span class="pln" > openFileInput</span><span class="pun" >(</span><span class="pln" >file</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > c</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > temp</span><span class="pun" >=</span><span class="str" >&quot;&quot;</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >while</span><span class="pun" >(</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >c </span><span class="pun" >=</span><span class="pln" > fin</span><span class="pun" >.</span><span class="pln" >read</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >!=</span><span class="pln" > </span><span class="pun" >-</span><span class="lit" >1</span><span class="pun" >){</span><span class="pln" > temp </span><span class="pun" >=</span><span class="pln" > temp </span><span class="pun" >+</span><span class="pln" > </span><span class="typ" >Character</span><span class="pun" >.</span><span class="pln" >toString</span><span class="pun" >((</span><span class="kwd" >char</span><span class="pun" >)</span><span class="pln" >c</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > tv</span><span class="pun" >.</span><span class="pln" >setText</span><span class="pun" >(</span><span class="pln" >temp</span><span class="pun" >);</span><span class="pln" > </span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >makeText</span><span class="pun" >(</span><span class="pln" >getBaseContext</span><span class="pun" >(),</span><span class="str" >&quot;file read&quot;</span><span class="pun" >,</span><span class="typ" >Toast</span><span class="pun" >.</span><span class="pln" >LENGTH_SHORT</span><span class="pun" >).</span><span class="pln" >show</span><span class="pun" >();</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >catch</span><span class="pun" >(</span><span class="typ" >Exception</span><span class="pln" > e</span><span class="pun" >){</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Inflate the menu; this adds items to the action bar if it is present.</span><span class="pln" > getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >menu_main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onOptionsItemSelected</span><span class="pun" >(</span><span class="typ" >MenuItem</span><span class="pln" > item</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Handle action bar item clicks here. The action bar will</span><span class="pln" > </span><span class="com" >// automatically handle clicks on the Home/Up button, so long</span><span class="pln" > </span><span class="com" >// as you specify a parent activity in AndroidManifest.xml.</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > id </span><span class="pun" >=</span><span class="pln" > item</span><span class="pun" >.</span><span class="pln" >getItemId</span><span class="pun" >();</span><span class="pln" > </span><span class="com" >//noinspection SimplifiableIfStatement</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >id </span><span class="pun" >==</span><span class="pln" > R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >action_settings</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onOptionsItemSelected</span><span class="pun" >(</span><span class="pln" >item</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >Following is the modified content of the xml<b >res/layout/activity_main.xml</b>.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="tag" >&lt;RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/tools&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Internal storage&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textview&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;35dp&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Tutorials point&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textview&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >&quot;#ff7aff24&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;35dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;Button</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Save&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentBottom</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;EditText</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/editText&quot;</span><span class="pln" > </span><span class="atn" >android:hint</span><span class="pun" >=</span><span class="atv" >&quot;Enter Text&quot;</span><span class="pln" > </span><span class="atn" >android:focusable</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textColorHighlight</span><span class="pun" >=</span><span class="atv" >&quot;#ff7eff15&quot;</span><span class="pln" > </span><span class="atn" >android:textColorHint</span><span class="pun" >=</span><span class="atv" >&quot;#ffff25e6&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_marginTop</span><span class="pun" >=</span><span class="atv" >&quot;42dp&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;ImageView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/abc&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;Button</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;load&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignTop</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >&quot;@+id/editText&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >&quot;@+id/editText&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Read&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/editText&quot;</span><span class="pln" > </span><span class="atn" >android:layout_toLeftOf</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_toStartOf</span><span class="pun" >=</span><span class="atv" >&quot;@+id/button2&quot;</span><span class="pln" > </span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >&quot;#ff5bff1f&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;25dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/RelativeLayout&gt;</span></pre> <p >Following is the content of the<b >res/values/string.xml</b>.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="tag" >&lt;resources&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;app_name&quot;</span><span class="tag" >&gt;</span><span class="pln" >My Application</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;hello_world&quot;</span><span class="tag" >&gt;</span><span class="pln" >Hello world!</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;action_settings&quot;</span><span class="tag" >&gt;</span><span class="pln" >Settings</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;/resources&gt;</span></pre> <p >Following is the content of<b >AndroidManifest.xml</b>file.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pun" >&lt;?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >&quot;1.0&quot;</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >&quot;utf-8&quot;</span><span class="pun" >?&gt;</span><span class="pln" > </span><span class="tag" >&lt;manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >&quot;com.example.sairamkrishna.myapplication&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@mipmap/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;activity</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.action.MAIN&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.category.LAUNCHER&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/activity&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span><span class="pln" > </span><span class="tag" >&lt;/manifest&gt;</span></pre> <p >Let&#39;s try to run our Storage application we just modified. I assume you had created your<b >AVD</b>while doing environment setup. To run the app from Android studio, open one of your project&#39;s activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.jpg" />icon from the tool bar. Android studio installs the app on your AVD and starts it and if everything is fine with your set-up and application, it will display following Emulator window &minus;</p> <p><img alt="Android Internal Stroage Tutorial" src="http://www.tutorialspoint.com/android/images/internal_storage.jpg" /></p> <p >Now what you need to do is to enter any text in the field. For example , i have entered some text. Press the save button. The following notification would appear in you AVD &minus;</p> <p><img alt="Android Internal Stroage Tutorial" src="http://www.tutorialspoint.com/android/images/internal_storage_save.jpg" /></p> <p >Now when you press the load button, the application will read the file , and display the data. In case of our, following data would be returned &minus;</p> <p><img alt="Android Internal Stroage Tutorial" src="http://www.tutorialspoint.com/android/images/internal_storage_read.jpg" /></p> <p >Note you can actually view this file by switching to DDMS tab. In DDMS , select file explorer and navigate this path.</p> <pre class="result notranslate" > tools&gt;android&gt;android device Monitor </pre> <p >This has also been shown in the image below.</p> <p><img alt="Android Internal Stroage Tutorial" src="http://www.tutorialspoint.com/android/images/ddms_internal.jpg" /></p> <div></div>
RangeTime:0.007012s
RangeMem:247.59 KB
返回顶部 留言