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. <!-- Take an EditText that contains the file name and this file name will be got by calling getString() -->
  8. <EditText
  9. android:id="@+id/editText1"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_alignParentRight="true"
  13. android:layout_alignParentTop="true"
  14. android:layout_marginRight="20dp"
  15. android:layout_marginTop="24dp"
  16. android:ems="10" >
  17. <requestFocus />
  18. </EditText>
  19. <!-- Take an EditText that contains the date and this date will be got by calling getString() -->
  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. <!-- This Textiew is used to diplay the text (filename -->
  29. <TextView
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_alignBaseline="@+id/editText1"
  33. android:layout_alignBottom="@+id/editText1"
  34. android:layout_alignParentLeft="true"
  35. android:text="File Name:" />
  36. <!-- This Textiew is used to diplay the text(date) -->
  37. <TextView
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_alignBaseline="@+id/editText2"
  41. android:layout_alignBottom="@+id/editText2"
  42. android:layout_alignParentLeft="true"
  43. android:text="Data:" />
  44. <!-- This button is used to perform click event that saves the file to the internal storage -->
  45. <Button
  46. android:id="@+id/buttonSave"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_marginLeft="70dp"
  50. android:layout_marginTop="150dp"
  51. android:text="save" />
  52. <!-- This button is used to perform click event that read the file from the internal storage -->
  53. <Button
  54. android:id="@+id/buttonRead"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_marginLeft="140dp"
  58. android:layout_marginTop="150dp"
  59. android:text="read" />
  60. </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. import android.os.Bundle;
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.view.Menu;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17. public class MainActivity extends Activity
  18. {
  19. EditText editText1, editText2;
  20. Button Buttonsave, Buttonread;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState)
  23. {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. // create the id of edittext and buttons
  27. editText1 = (EditText) findViewById(R.id.editText1);
  28. editText2 = (EditText) findViewById(R.id.editText2);
  29. Buttonsave = (Button) findViewById(R.id.buttonSave);
  30. Buttonread = (Button) findViewById(R.id.buttonRead);
  31. //set the button on its clickListener and fetch the file name and date from the edittext
  32. Buttonsave.setOnClickListener(new OnClickListener()
  33. {
  34. @Override
  35. public void onClick(View arg0)
  36. {
  37. String file = editText1.getText().toString();
  38. String data = editText2.getText().toString();
  39. // 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.
  40. FileOutputStream fileoutputStream;
  41. try
  42. {
  43. fileoutputStream = openFileOutput(file, Context.MODE_PRIVATE);
  44. fileoutputStream.write(data.getBytes());
  45. fileoutputStream.close();
  46. Toast.makeText(getApplicationContext(), file + " saved",
  47. Toast.LENGTH_LONG).show();
  48. }
  49. catch (FileNotFoundException e)
  50. {
  51. e.printStackTrace();
  52. }
  53. catch (IOException e)
  54. {
  55. e.printStackTrace();
  56. }
  57. }
  58. });
  59. //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.
  60. Buttonread.setOnClickListener(new OnClickListener()
  61. {
  62. @Override
  63. public void onClick(View arg0)
  64. {
  65. String file = editText1.getText().toString();
  66. StringBuffer stringBuffer = new StringBuffer();
  67. try
  68. {
  69. BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput(file)));
  70. String inputStr;
  71. while ((inputStr = inputReader.readLine()) != null)
  72. {
  73. stringBuffer.append(inputStr + "\n");
  74. }
  75. }
  76. catch (IOException e)
  77. {
  78. e.printStackTrace();
  79. }
  80. Toast.makeText(getApplicationContext(), stringBuffer.toString(),
  81. Toast.LENGTH_LONG).show();
  82. }
  83. });
  84. }
  85. @Override
  86. public boolean onCreateOptionsMenu(Menu menu)
  87. {
  88. getMenuInflater().inflate(R.menu.main, menu);
  89. return true;
  90. }
  91. }
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. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="18" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.internalstorageapp.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 5
internal
insert filename and date to save the data.
internal1
internal2