Internal Storage in Android

Introduction

 
This article explains internal 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.
 
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 the data of the files.
 
FileInputStream
 
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(): this method reads a single byte from this Stream and returns this as an integer in the range from 0 to 225
  • public void close(): this method closes the stream.
FileOutPutStream
 
FileOutputStraem is an output stream for writing data to a file. It extends the OutputStream class. Methods provided by the outputStream class are:
  • public void close(): This method is used to close the stream
  • public void write(): This method is used to write a single byte to the stream
Step 1
 
Create a project like this:
 
internalstorage
 
Step 2
 
Create an XML file and write the following:
  1. <!-- In this you will take a Relative Layout that display the elements in relative position  -->       
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     xmlns:tools="http://schemas.android.com/tools"   
  4.     android:layout_width="match_parent"   
  5.     android:layout_height="match_parent"   
  6.     tools:context=".MainActivity" >   
  7.   
  8.  <!-- Take an EditText that contains the file name and this file name will be got by calling getString()  -->  
  9.   
  10.      <EditText   
  11.         android:id="@+id/editText1"   
  12.         android:layout_width="wrap_content"   
  13.         android:layout_height="wrap_content"   
  14.         android:layout_alignParentRight="true"   
  15.         android:layout_alignParentTop="true"   
  16.         android:layout_marginRight="20dp"   
  17.         android:layout_marginTop="24dp"   
  18.         android:ems="10" >   
  19.    
  20.         <requestFocus />   
  21.     </EditText>   
  22.    
  23. <!-- Take an EditText that contains the date and this date will be got by calling getString()  -->  
  24.     <EditText   
  25.         android:id="@+id/editText2"   
  26.         android:layout_width="wrap_content"   
  27.         android:layout_height="wrap_content"   
  28.         android:layout_alignRight="@+id/editText1"   
  29.         android:layout_below="@+id/editText1"   
  30.         android:layout_marginTop="24dp"   
  31.         android:ems="10" />   
  32.    
  33. <!-- This Textiew is used to diplay the text (filename -->    
  34.   
  35.     <TextView   
  36.         android:layout_width="wrap_content"   
  37.         android:layout_height="wrap_content"   
  38.         android:layout_alignBaseline="@+id/editText1"   
  39.         android:layout_alignBottom="@+id/editText1"   
  40.         android:layout_alignParentLeft="true"   
  41.         android:text="File Name:" />   
  42.   
  43. <!-- This Textiew is used to diplay the text(date)  -->    
  44.   
  45.     <TextView   
  46.         android:layout_width="wrap_content"   
  47.         android:layout_height="wrap_content"   
  48.         android:layout_alignBaseline="@+id/editText2"   
  49.         android:layout_alignBottom="@+id/editText2"   
  50.         android:layout_alignParentLeft="true"   
  51.         android:text="Data:" />   
  52.   
  53. <!-- This button is used to perform click event that saves the file to the internal storage  -->      
  54.   
  55.     <Button   
  56.         android:id="@+id/buttonSave"   
  57.         android:layout_width="wrap_content"   
  58.         android:layout_height="wrap_content"    
  59.         android:layout_marginLeft="70dp"   
  60.         android:layout_marginTop="150dp"  
  61.         android:text="save" />   
  62.    
  63. <!-- This button is used to perform click event that read the file from the internal storage  -->      
  64.   
  65.     <Button   
  66.         android:id="@+id/buttonRead"   
  67.         android:layout_width="wrap_content"   
  68.         android:layout_height="wrap_content"  
  69.           android:layout_marginLeft="140dp"   
  70.         android:layout_marginTop="150dp"  
  71.         android:text="read" />   
  72.    
  73. </RelativeLayout> 
Step 3
 
Create a Java class file and write the following:
 
A package is a group of classes, subpackages, and interfaces. I used various packages in this application.
 
I imported java.io.BuffeReader that provides the BufferReader class. This class provides the facility to read text from a character input stream.
 
java.io.FileInputStream provides the FileInputStream class that 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. 
 
java.io.FileOutputStream provides the FileOutputStream class is an output stream for writing data to a file. It extends the OutputStream class.
  • android.app.Activity provides methods such as onCreate() to create the activity and to display the activity on the screen.
  • android.widgets.buttons provide you the facility to use buttons in your Activity.
  • android.widgets.toast provides you the facility to use the Toast class in your application. Using the toast class you can use toast notification to display a message. 
  1. package com.internalstorageapp;  
  2. import java.io.BufferedReader;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8.   
  9. import android.os.Bundle;  
  10. import android.app.Activity;  
  11. import android.content.Context;  
  12. import android.view.Menu;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.EditText;  
  17. import android.widget.Toast;  
  18.   
  19. public class MainActivity extends Activity   
  20. {  
  21.  EditText editText1, editText2;  
  22.  Button Buttonsave, Buttonread;  
  23.  @Override  
  24.  protected void onCreate(Bundle savedInstanceState)   
  25.  {  
  26.   super.onCreate(savedInstanceState);  
  27.   setContentView(R.layout.activity_main);  
  28.   
  29.   // create the id of edittext and buttons  
  30.   
  31.   editText1 = (EditText) findViewById(R.id.editText1);  
  32.   editText2 = (EditText) findViewById(R.id.editText2);  
  33.   Buttonsave = (Button) findViewById(R.id.buttonSave);  
  34.   Buttonread = (Button) findViewById(R.id.buttonRead);  
  35.   
  36.   //set the button on its clickListener and fetch the file name and date from the edittext  
  37.   
  38.   Buttonsave.setOnClickListener(new OnClickListener()   
  39.   {  
  40.   
  41.    @Override  
  42.    public void onClick(View arg0)   
  43.    {  
  44.     String file = editText1.getText().toString();  
  45.     String data = editText2.getText().toString();  
  46.   
  47.     // here you will create a variable of FileOutputStream that holds the outputStraem. Toast will show you a message that contains file name with saved text. This toast will show you the  information regarding file.  
  48.   
  49.     FileOutputStream fileoutputStream;  
  50.     try   
  51.     {  
  52.      fileoutputStream = openFileOutput(file, Context.MODE_PRIVATE);  
  53.      fileoutputStream.write(data.getBytes());  
  54.      fileoutputStream.close();  
  55.   
  56.      Toast.makeText(getApplicationContext(), file + " saved",  
  57.       Toast.LENGTH_LONG).show();  
  58.   
  59.     }   
  60.     catch (FileNotFoundException e)   
  61.     {  
  62.      e.printStackTrace();  
  63.     }   
  64.     catch (IOException e)   
  65.     {  
  66.      e.printStackTrace();  
  67.     }  
  68.    }  
  69.   });  
  70.   //set the button on its clikListener and fetch the file name and date from the edittext . On Click of button you will read the file name from the internal storage.   
  71.   
  72.   Buttonread.setOnClickListener(new OnClickListener()   
  73.   {  
  74.   
  75.    @Override  
  76.    public void onClick(View arg0)   
  77.    {  
  78.     String file = editText1.getText().toString();  
  79.     StringBuffer stringBuffer = new StringBuffer();  
  80.     try   
  81.     {  
  82.      BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput(file)));  
  83.      String inputStr;  
  84.      while ((inputStr = inputReader.readLine()) != null)   
  85.      {  
  86.       stringBuffer.append(inputStr + "\n");  
  87.      }  
  88.   
  89.     }   
  90.     catch (IOException e)   
  91.     {  
  92.      e.printStackTrace();  
  93.     }  
  94.     Toast.makeText(getApplicationContext(), stringBuffer.toString(),  
  95.      Toast.LENGTH_LONG).show();  
  96.    }  
  97.   });  
  98.  }  
  99.  @Override  
  100.  public boolean onCreateOptionsMenu(Menu menu)   
  101.  {  
  102.   getMenuInflater().inflate(R.menu.main, menu);  
  103.   return true;  
  104.  }  
  105.   
Step 4
 
Android Manifest.xml file
 
This is an Android manifest.xml file that works as a bridge between developers and the Android System. This file contains information about the package and the component.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.internalstorageapp"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.internalstorageapp.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 5
 
internal
 
insert filename and date to save the data. 
 
internal1
 
internal2


Similar Articles