AdSense

AdSense3

Thursday, 1 October 2015

Android Internal Storage Example

We are able to save or read data from the device internal memory. FileInputStream and FileOutputStream classes are used to read and write data into the file.

Here, we are going to read and write data to the internal storage of the device.

Example of reading and writing data to the android internal storage

activity_main.xml

Drag the 2 edittexts, 2 textviews and 2 buttons from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginRight="20dp"  
  14.         android:layout_marginTop="24dp"  
  15.         android:ems="10" >  
  16.   
  17.         <requestFocus />  
  18.     </EditText>  
  19.   
  20.     <EditText  
  21.         android:id="@+id/editText2"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_alignRight="@+id/editText1"  
  25.         android:layout_below="@+id/editText1"  
  26.         android:layout_marginTop="24dp"  
  27.         android:ems="10" />  
  28.   
  29.     <TextView  
  30.         android:id="@+id/textView1"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_alignBaseline="@+id/editText1"  
  34.         android:layout_alignBottom="@+id/editText1"  
  35.         android:layout_alignParentLeft="true"  
  36.         android:text="File Name:" />  
  37.   
  38.     <TextView  
  39.         android:id="@+id/textView2"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_alignBaseline="@+id/editText2"  
  43.         android:layout_alignBottom="@+id/editText2"  
  44.         android:layout_alignParentLeft="true"  
  45.         android:text="Data:" />  
  46.   
  47.     <Button  
  48.         android:id="@+id/button1"  
  49.         android:layout_width="wrap_content"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_alignLeft="@+id/editText2"  
  52.         android:layout_below="@+id/editText2"  
  53.         android:layout_marginLeft="70dp"  
  54.         android:layout_marginTop="16dp"  
  55.         android:text="save" />  
  56.   
  57.     <Button  
  58.         android:id="@+id/button2"  
  59.         android:layout_width="wrap_content"  
  60.         android:layout_height="wrap_content"  
  61.         android:layout_alignBaseline="@+id/button1"  
  62.         android:layout_alignBottom="@+id/button1"  
  63.         android:layout_toRightOf="@+id/button1"  
  64.         android:text="read" />  
  65.   
  66. </RelativeLayout>  

Activity class

Let's write the code to write and read data from the internal storage.
File: MainActivity.java
  1. package com.example.internalstorage;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9.   
  10. import android.os.Bundle;  
  11. import android.app.Activity;  
  12. import android.content.Context;  
  13. import android.view.Menu;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.EditText;  
  18. import android.widget.Toast;  
  19.   
  20. public class MainActivity extends Activity {  
  21.     EditText editTextFileName,editTextData;  
  22.     Button saveButton,readButton;  
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.           
  28.         editTextFileName=(EditText)findViewById(R.id.editText1);  
  29.         editTextData=(EditText)findViewById(R.id.editText2);  
  30.         saveButton=(Button)findViewById(R.id.button1);  
  31.         readButton=(Button)findViewById(R.id.button2);  
  32.           
  33.         //Performing Action on Read Button  
  34.         saveButton.setOnClickListener(new OnClickListener(){  
  35.   
  36.             @Override  
  37.             public void onClick(View arg0) {  
  38.                 String filename=editTextFileName.getText().toString();  
  39.                 String data=editTextData.getText().toString();  
  40.                   
  41.                 FileOutputStream fos;  
  42.                    try {  
  43.                     fos = openFileOutput(filename, Context.MODE_PRIVATE);  
  44.                     //default mode is PRIVATE, can be APPEND etc.  
  45.                     fos.write(data.getBytes());  
  46.                     fos.close();  
  47.                      
  48.                     Toast.makeText(getApplicationContext(),filename + " saved",  
  49.                             Toast.LENGTH_LONG).show();  
  50.                       
  51.                      
  52.                    } catch (FileNotFoundException e) {e.printStackTrace();}  
  53.                    catch (IOException e) {e.printStackTrace();}  
  54.                   
  55.             }  
  56.               
  57.         });  
  58.           
  59.         //Performing Action on Read Button  
  60.         readButton.setOnClickListener(new OnClickListener(){  
  61.   
  62.             @Override  
  63.             public void onClick(View arg0) {  
  64.                 String filename=editTextFileName.getText().toString();  
  65.                 StringBuffer stringBuffer = new StringBuffer();    
  66.                 try {  
  67.                     //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader  
  68.                     BufferedReader inputReader = new BufferedReader(new InputStreamReader(  
  69.                             openFileInput(filename)));  
  70.                     String inputString;  
  71.                     //Reading data line by line and storing it into the stringbuffer                
  72.                     while ((inputString = inputReader.readLine()) != null) {  
  73.                         stringBuffer.append(inputString + "\n");  
  74.                     }  
  75.                       
  76.                 } catch (IOException e) {  
  77.                     e.printStackTrace();  
  78.                 }  
  79.                 //Displaying data on the toast  
  80.                 Toast.makeText(getApplicationContext(),stringBuffer.toString(),  
  81.                         Toast.LENGTH_LONG).show();  
  82.                   
  83.             }  
  84.               
  85.         });  
  86.     }  
  87.   
  88.     @Override  
  89.     public boolean onCreateOptionsMenu(Menu menu) {  
  90.         // Inflate the menu; this adds items to the action bar if it is present.  
  91.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  92.         return true;  
  93.     }  
  94.   
  95. }  

1 comment: