External Storage in Android

Introduction

 
This article explains External Storage in Android.
 
Android provides many options to store application data. The choice depends on your needs, such as whether the data should be accessible from other applications and how much space your data needs.
 
We can also store data to and read data from the external storage just like internal storage. The "FileInputStream" and "FileOutPutStream" classes are used to save and read data into the files.
 
External Storage
 
Every Android device provides storage space to save files known as external storage. Data from the external storage can be removed and can be modifiable by the user.
 
Internal storage
 
 
Store private data on the device memory. You can directly save your application data to the internal memory. By default data is saved in internal memory and other applications cannot access it. When you uninstall your application all files will be removed from the device. 
 
The FileInoutStream and FileOutPutStream classes are used to read and write data into the files.
 
FileInputStream
 
The FileInputStream gets input bytes from a file. The FileInputStream class provides the facility to read streams of raw bytes such as image data. It extends the InputStream class. The following are methods provided by the fileInputStream class:
  • public int read()
    reads a single byte from this Stream and returns it as an integer in the range from 0 to 225.
  • public void close()
    closes the stream.
FileOutPutStream
 
FileOutputStraem is an output stream for writing data to a file. It extends the OutputStream class. The following are methods provided by the outputStream class:
  • public void close()
    closes the stream.
  • public void write()
    writes a single byte to the stream.
Step 1
 
ext1
 
Step 2
 
Create an XML file with this:
  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.     <LinearLayout  
  8.         android:id="@+id/linearlayout1"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal"  
  11.         android:layout_width="fill_parent">  
  12.     <TextView   
  13.         android:id="@+id/textView1"   
  14.         android:layout_width="wrap_content"   
  15.         android:layout_height="wrap_content"   
  16.         android:text="File Name:" />   
  17.    
  18.          
  19.         <EditText   
  20.         android:id="@+id/edtText1"   
  21.         android:layout_width="wrap_content"   
  22.         android:layout_height="wrap_content"   
  23.         android:layout_marginLeft="20dp"  
  24.         android:ems="10" >   
  25.    </EditText>  
  26.         
  27.     </LinearLayout>  
  28.  <LinearLayout  
  29.      android:layout_below="@+id/linearlayout1"  
  30.         android:layout_height="wrap_content"  
  31.         android:orientation="horizontal"  
  32.         android:layout_width="fill_parent">       
  33.      
  34.       <TextView   
  35.         android:id="@+id/textView2"   
  36.         android:layout_width="wrap_content"   
  37.         android:layout_height="wrap_content"   
  38.         android:text="Data:" />   
  39.    
  40.     <EditText   
  41.         android:id="@+id/edtText2"   
  42.         android:layout_width="wrap_content"   
  43.         android:layout_height="wrap_content"   
  44.         android:layout_marginLeft="50dp"  
  45.         android:ems="10" />    
  46.     
  47.     </LinearLayout>  
  48.     
  49.    
  50.  <Button   
  51.         android:id="@+id/save_btn"   
  52.         android:layout_width="wrap_content"   
  53.         android:layout_height="wrap_content"   
  54.         android:layout_marginLeft="170dp"   
  55.         android:layout_marginTop="150dp"   
  56.         android:text="save" />   
  57.    
  58.     <Button   
  59.         android:id="@+id/read_btn"   
  60.         android:layout_marginLeft="50dp"  
  61.         android:layout_marginTop="150dp"  
  62.         android:layout_width="wrap_content"   
  63.         android:layout_height="wrap_content"   
  64.         android:text="read" />   
  65.    
  66. </RelativeLayout>   
Step 3
 
Create a Java class file with this:
  1. package com.compsapp;  
  2.    
  3. import java.io.BufferedReader;   
  4. import java.io.File;   
  5. import java.io.FileInputStream;   
  6. import java.io.FileNotFoundException;   
  7. import java.io.FileOutputStream;   
  8. import java.io.IOException;   
  9. import java.io.InputStreamReader;   
  10. import java.io.OutputStreamWriter;   
  11. import android.os.Bundle;   
  12. import android.app.Activity;   
  13. import android.content.Context;   
  14. import android.view.Menu;   
  15. import android.view.View;   
  16. import android.view.View.OnClickListener;   
  17. import android.widget.Button;   
  18. import android.widget.EditText;   
  19. import android.widget.Toast;   
  20.    
  21. public class MainActivity extends Activity {   
  22.     EditText edt_FileName,edt_Data;   
  23.     Button save_btn,read_btn;   
  24.     @Override   
  25.     protected void onCreate(Bundle savedInstanceState) {   
  26.         super.onCreate(savedInstanceState);   
  27.         setContentView(R.layout.activity_main);   
  28.    
  29.         edt_FileName=(EditText)findViewById(R.id.edtText1);   
  30.         edt_Data=(EditText)findViewById(R.id.edtText2);   
  31.         save_btn=(Button)findViewById(R.id.save_btn);   
  32.         read_btn=(Button)findViewById(R.id.read_btn);   
  33.    
  34.         //Performing action on save button   
  35.         save_btn.setOnClickListener(new OnClickListener(){   
  36.    
  37.             @Override   
  38.             public void onClick(View arg0) {   
  39.                 String filename=edt_FileName.getText().toString();   
  40.                 String data=edt_Data.getText().toString();   
  41.    
  42.                 FileOutputStream fos;   
  43.                    try {   
  44.                        File my_file = new File("/sdcard/"+filename);   
  45.                         my_file.createNewFile();   
  46.                         FileOutputStream fileOutputStream = new FileOutputStream(my_file);   
  47.                         OutputStreamWriter OutWriter = new OutputStreamWriter(fileOutputStream);   
  48.                         OutWriter.append(data);   
  49.                         OutWriter.close();   
  50.                         fileOutputStream.close();   
  51.    
  52.             Toast.makeText(getApplicationContext(),filename + " saved",Toast.LENGTH_LONG).show();       
  53.                    } catch (FileNotFoundException e) {e.printStackTrace();}   
  54.                    catch (IOException e) {e.printStackTrace();}      
  55.             }   
  56.         });   
  57.    
  58.         //Performing action on Read Button   
  59.         read_btn.setOnClickListener(new OnClickListener(){   
  60.    
  61.             @Override   
  62.             public void onClick(View arg0) {   
  63.                 String filename=edt_FileName.getText().toString();   
  64.                 StringBuffer Buffer = new StringBuffer();     
  65.                 String DataRow = "";   
  66.                 String aBuffer = "";   
  67.                 try {   
  68.                     File myfile = new File("/sdcard/"+filename);   
  69.                     FileInputStream fIn = new FileInputStream(myfile);   
  70.                     BufferedReader bufferedReader = new BufferedReader(   
  71.                             new InputStreamReader(fIn));   
  72.    
  73.                     while ((DataRow = bufferedReader.readLine()) != null) {   
  74.                         aBuffer += DataRow + "\n";   
  75.                     }   
  76.                     bufferedReader.close();   
  77.    
  78.                 } catch (IOException e) {   
  79.                     e.printStackTrace();   
  80.                 }   
  81.                 Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();   
  82.             }   
  83.         });   
  84.     }   
  85.    
  86.     @Override   
  87.     public boolean onCreateOptionsMenu(Menu menu) {   
  88.         // Inflate the menu; this adds items to the action bar if it is present.   
  89.         getMenuInflater().inflate(R.menu.main, menu);   
  90.         return true;   
  91.     }   
  92.    
  93. }   
Step 4
 
In the Android Manifest. xml file give the external storage permission as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.compsapp"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6. <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="18" />  
  9.      
  10.     <application  
  11.         android:allowBackup="true"  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name"  
  14.         android:theme="@style/AppTheme" >  
  15.         <activity  
  16.             android:name="com.compsapp.MainActivity"  
  17.             android:label="@string/app_name" >  
  18.             <intent-filter>  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.    
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>   
  26. </manifest> 
Step 5
 
ext2


Similar Articles