Android Internal Storage Example(Read/Write)

By | December 25, 2013

In this tutorial we will see  about android internal file storage , Internal Storage are private to your application and other applications cannot access them .and  when the user uninstalls the applications the internal stored files associated with it are also removed

Android Internal Storage

Here In this tutorial we will see a sample example in which firstly we get the data entered by user from EditText and save this data when Save Button is clicked and finally read the data which is saved into the file and display it to TextView when Load Button is clicked .

For writing and reading to and from a file we are making using of FileOutputStream  and  FileInputStream classes of java.io  package respectively .

Project Detail

Project Name InternalStorage
Package com.pavan.internalstorage
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

<RelativeLayout 
    xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:paddingBottom=”@dimen/activity_vertical_margin”
    android:paddingLeft=”@dimen/activity_horizontal_margin”
    android:paddingRight=”@dimen/activity_horizontal_margin”
    android:paddingTop=”@dimen/activity_vertical_margin”
    tools:context=”.MainActivity” >

    <EditText
        android:id=”@+id/et_data_id”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignParentTop=”true”
        android:layout_centerHorizontal=”true”
        android:layout_marginTop=”25dp”
        android:ems=”10″ >

        <requestFocus />
    </EditText>

    <Button
        android:id=”@+id/load_bt_id”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignBaseline=”@+id/save_bt_id”
        android:layout_alignBottom=”@+id/save_bt_id”
        android:layout_alignRight=”@+id/et_data_id”
        android:text=”Load”
        android:visibility=”invisible” />

    <Button
        android:id=”@+id/save_bt_id”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_alignLeft=”@+id/et_data_id”
        android:layout_below=”@+id/et_data_id”
        android:layout_marginTop=”25dp”
        android:text=”Save” />

    <TextView
        android:id=”@+id/tv_data_id”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_below=”@+id/load_bt_id”
        android:layout_centerHorizontal=”true”
        android:layout_marginTop=”15dp”
        android:text=””
        android:textSize=”20dp” />

</RelativeLayout>

2. Activity

package com.pavan.internalstorage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

 EditText et_data;
 Button save_bt, load_bt;
 TextView tv_data;

 String FILENAME = “myfile”;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  initializeViews();

  save_bt.setOnClickListener(this);
  load_bt.setOnClickListener(this);

 }

 private void initializeViews() {
  et_data = (EditText) findViewById(R.id.et_data_id);
  save_bt = (Button) findViewById(R.id.save_bt_id);
  load_bt = (Button) findViewById(R.id.load_bt_id);
  tv_data = (TextView) findViewById(R.id.tv_data_id);

 }

 @Override
 public void onClick(View v) {
  switch (v.getId()) {

  // Writing Data to file
  case R.id.save_bt_id:
   String data = et_data.getText().toString();
   int len = data.length();

   if (len == 0) {
    Toastmessage(“Please Enter The Data”);

   } else {
    try {
     FileOutputStream fos = openFileOutput(FILENAME,
       Context.MODE_PRIVATE);
     fos.write(data.getBytes());
     fos.close();

     Toastmessage(“Data Saved Inside File ” + FILENAME);
     //set visible for load button when data is written to file
     load_bt.setVisibility(View.VISIBLE);

    } catch (FileNotFoundException e) {

     e.printStackTrace();
    } catch (IOException e) {

     e.printStackTrace();
    }
   }

   break;

  // Reading Data From File
  case R.id.load_bt_id:
   String read_data = null;

   try {
    FileInputStream fis = openFileInput(FILENAME);
    byte[] dataArray = new byte[fis.available()];
    while (fis.read(dataArray) != -1) {
     read_data = new String(dataArray);
    }
    fis.close();

    tv_data.setText(read_data);

   } catch (FileNotFoundException e) {

    e.printStackTrace();
   } catch (IOException e) {

    e.printStackTrace();
   }

   break;
  }

 }

 void Toastmessage(String msg) {
  Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
 }

}

3. RUN

Android Internal Storage Read Write
  • Anonymous

    How do i search myfile file for my data added ?

  • Anonymous

    where will be the myfile file?and can i use this internal memory for storing and retriving login credentials?

  • Great Example thanks Android Basic Tutorial