Quick Contact


    Android Internal Storage

    From the device’s internal memory, we may save or read data. To read and write data to a file, the FileInputStream and FileOutputStream classes are needed. We’ll be reading and writing data to the device’s internal storage.Android provides a variety of storage options for apps to store their data. Shared preferences, internal and external storage, SQLite storage, and storage through network connection are all examples of storage locations.

    The internal storage will be discussed in this chapter. Internal storage refers to the storing of personal information on the device’s memory.

    By default, these files are private and may only be viewed by your programme, and they are erased when the user deletes it.

    activity_main.xml

    < RelativeLayoutxmlns: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”

    tools:context=”.MainActivity” >

    < EditText

    android:id=”@+id/editText1″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignParentRight=”true”

    android:layout_alignParentTop=”true”

    android:layout_marginRight=”20dp”

    android:layout_marginTop=”24dp”

    android:ems=”10″ >

    < requestFocus />

    < /EditText>

    < EditText

    android:id=”@+id/editText2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignRight=”@+id/editText1″

    android:layout_below=”@+id/editText1″

    android:layout_marginTop=”24dp”

    android:ems=”10″ />

    < TextView

    android:id=”@+id/textView1″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignBaseline=”@+id/editText1″

    android:layout_alignBottom=”@+id/editText1″

    android:layout_alignParentLeft=”true”

    android:text=”File Name:” />

    < TextView

    android:id=”@+id/textView2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignBaseline=”@+id/editText2″

    android:layout_alignBottom=”@+id/editText2″

    android:layout_alignParentLeft=”true”

    android:text=”Data:” />

    < Button

    android:id=”@+id/button1″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignLeft=”@+id/editText2″

    android:layout_below=”@+id/editText2″

    android:layout_marginLeft=”70dp”

    android:layout_marginTop=”16dp”

    android:text=”save” />

    < Button

    android:id=”@+id/button2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignBaseline=”@+id/button1″

    android:layout_alignBottom=”@+id/button1″

    android:layout_toRightOf=”@+id/button1″

    android:text=”read” />

    < EditText

    android:id=”@+id/editText1″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignParentRight=”true”

    android:layout_alignParentTop=”true”

    android:layout_marginRight=”20dp”

    android:layout_marginTop=”24dp”

    android:ems=”10″ >

    < requestFocus />

    < /EditText>

    < EditText

    android:id=”@+id/editText2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignRight=”@+id/editText1″

    android:layout_below=”@+id/editText1″

    android:layout_marginTop=”24dp”

    android:ems=”10″ />

    < TextView

    android:id=”@+id/textView1″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignBaseline=”@+id/editText1″

    android:layout_alignBottom=”@+id/editText1″

    android:layout_alignParentLeft=”true”

    android:text=”File Name:” />

    < TextView

    android:id=”@+id/textView2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignBaseline=”@+id/editText2″

    android:layout_alignBottom=”@+id/editText2″

    android:layout_alignParentLeft=”true”

    android:text=”Data:” />

    < Button2

    android:id=”@+id/button1″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignLeft=”@+id/editText2″

    android:layout_below=”@+id/editText2″

    android:layout_marginLeft=”70dp”

    android:layout_marginTop=”16dp”

    android:text=”save” />

    < Button3

    android:id=”@+id/button2″

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_alignBaseline=”@+id/button1″

    android:layout_alignBottom=”@+id/button1″

    android:layout_toRightOf=”@+id/button1″

    android:text=”read” />

    < /RelativeLayout>

    Activity class

    package tutorials.ducatindia.com.internalstorage;

    import android.content.Context;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.EditText;

    import android.widget.Toast;

    import java.io.BufferedReader;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStreamReader;

    	public class MainActivity extends AppCompatActivity {  
    	EditTexteditTextFileName,editTextData;  
    	    Button saveButton,readButton;  
    	    @Override  
    	    protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	setContentView(R.layout.activity_main);  
    
    	editTextFileName=findViewById(R.id.editText1);  
    	editTextData=findViewById(R.id.editText2);  
    	saveButton=findViewById(R.id.button1);  
    	readButton=findViewById(R.id.button2);  
    
    	        //Performing Action on Read Button  
    	saveButton.setOnClickListener(new View.OnClickListener(){
    
    	            @Override  
    	            public void onClick(View arg0) {  
    	                String filename=editTextFileName.getText().toString();  
    	                String data=editTextData.getText().toString();  
    
    	FileOutputStreamfos;  
    	                try {  
    	fos = openFileOutput(filename, Context.MODE_PRIVATE);  
    	                    //default mode is PRIVATE, can be APPEND etc.  
    	fos.write(data.getBytes());  
    	fos.close();  
    
    	Toast.makeText(getApplicationContext(),filename + " saved",  
    	Toast.LENGTH_LONG).show();  
    
    
    	                } catch (FileNotFoundException e) {e.printStackTrace();}  
    	                catch (IOException e) {e.printStackTrace();}  
    
    	            }  
    
    	        });  
    
    	readButton.setOnClickListener(new View.OnClickListener(){
    
    	            @Override  
    	            public void onClick(View arg0) {  
    	                String filename=editTextFileName.getText().toString();  
    	StringBufferstringBuffer = new StringBuffer();  
    	                try {  
    	                    //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader
    	BufferedReaderinputReader = new BufferedReader(new InputStreamReader(  
    	openFileInput(filename)));  
    	                    String inputString;  
    	                    //Reading data line by line and storing it into the stringbuffer
    	                    while ((inputString = inputReader.readLine()) != null) {  
    	stringBuffer.append(inputString + "\n");  
    	                    }  
    
    	                } catch (IOException e) {  
    	e.printStackTrace();  
    	                }  
    
    	            Toast.makeText(getApplicationContext(),stringBuffer.toString(),Toast.LENGTH_LONG).show();  
    	public class MainActivity extends AppCompatActivity {  
    	EditTexteditTextFileName,editTextData;  
    	    Button saveButton,readButton;  
    	    @Override  
    	    protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	setContentView(R.layout.activity_main);  
    
    	editTextFileName=findViewById(R.id.editText1);  
    	editTextData=findViewById(R.id.editText2);  
    	saveButton=findViewById(R.id.button1);  
    	readButton=findViewById(R.id.button2);  
    
    	        //Performing Action on Read Button  
    	saveButton.setOnClickListener(new View.OnClickListener(){
    
    	            @Override  
    	            public void onClick(View arg0) {  
    	                String filename=editTextFileName.getText().toString();  
    	                String data=editTextData.getText().toString();  
    
    	FileOutputStreamfos;  
    	                try {  
    	fos = openFileOutput(filename, Context.MODE_PRIVATE);  
    	                    //default mode is PRIVATE, can be APPEND etc.  
    	fos.write(data.getBytes());  
    	fos.close();  
    
    	Toast.makeText(getApplicationContext(),filename + " saved",  
    	Toast.LENGTH_LONG).show();  
    
    
    	                } catch (FileNotFoundException e) {e.printStackTrace();}  
    	                catch (IOException e) {e.printStackTrace();}  
    
    	            }  
    
    	        });  
    
    	readButton.setOnClickListener(new View.OnClickListener(){
    
    	            @Override  
    	            public void onClick(View arg0) {  
    	                String filename=editTextFileName.getText().toString();  
    	StringBufferstringBuffer = new StringBuffer();  
    	                try {  
    	                    //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader
    	BufferedReaderinputReader = new BufferedReader(new InputStreamReader(  
    	openFileInput(filename)));  
    	                    String inputString;  
    	                    //Reading data line by line and storing it into the stringbuffer
    	                    while ((inputString = inputReader.readLine()) != null) {  
    	stringBuffer.append(inputString + "\n");  
    	                    }  
    
    	                } catch (IOException e) {  
    	e.printStackTrace();  
    	                }  
    
    	            Toast.makeText(getApplicationContext(),stringBuffer.toString(),Toast.LENGTH_LONG).show(); 
    
    	            }  
    
    	        });  
    	    }  
    	} 
    	
    Example 2: –

    < ?xml version="1.0" encoding="utf-8"?>

    < RelativeLayoutxmlns: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:paddingLeft=”@dimen/activity_horizontal_margin”

    android:paddingRight=”@dimen/activity_horizontal_margin”

    android:paddingTop=”@dimen/activity_vertical_margin”

    android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>

    < TextViewandroid:text="Internal storage" android:layout_width="wrap_content"

    android:layout_height=”wrap_content”

    android:id=”@+id/textview”

    android:textSize=”35dp”

    android:layout_alignParentTop=”true”

    android:layout_centerHorizontal=”true” />

    < TextView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”Tutorials point”

    android:id=”@+id/textView”

    android:layout_below=”@+id/textview”

    android:layout_centerHorizontal=”true”

    android:textColor=”#ff7aff24″

    android:textSize=”35dp” />

    < Button

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”Save”

    android:id=”@+id/button”

    android:layout_alignParentBottom=”true”

    android:layout_alignLeft=”@+id/textView”

    android:layout_alignStart=”@+id/textView” />

    < EditText

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/editText”

    android:hint=”Enter Text”

    android:focusable=”true”

    android:textColorHighlight=”#ff7eff15″

    android:textColorHint=”#ffff25e6″

    android:layout_below=”@+id/imageView”

    android:layout_alignRight=”@+id/textView”

    android:layout_alignEnd=”@+id/textView”

    android:layout_marginTop=”42dp”

    android:layout_alignLeft=”@+id/imageView”

    android:layout_alignStart=”@+id/imageView” />

    < ImageView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/imageView”

    android:src=”@drawable/abc”

    android:layout_below=”@+id/textView”

    android:layout_centerHorizontal=”true” />

    < Button

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”load”

    android:id=”@+id/button2″

    android:layout_alignTop=”@+id/button”

    android:layout_alignRight=”@+id/editText”

    android:layout_alignEnd=”@+id/editText” />

    < TextView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”Read”

    android:id=”@+id/textView2″

    android:layout_below=”@+id/editText”

    android:layout_toLeftOf=”@+id/button2″

    android:layout_toStartOf=”@+id/button2″

    android:textColor=”#ff5bff1f”

    android:textSize=”25dp” />

    < TextView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”Tutorials point”

    android:id=”@+id/textView”

    android:layout_below=”@+id/textview”

    android:layout_centerHorizontal=”true”

    android:textColor=”#ff7aff24″

    android:textSize=”35dp” />

    < Button

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”Save”

    android:id=”@+id/button”

    android:layout_alignParentBottom=”true”

    android:layout_alignLeft=”@+id/textView”

    android:layout_alignStart=”@+id/textView” />

    < EditText

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/editText”

    android:hint=”Enter Text”

    android:focusable=”true”

    android:textColorHighlight=”#ff7eff15″

    android:textColorHint=”#ffff25e6″

    android:layout_below=”@+id/imageView”

    android:layout_alignRight=”@+id/textView”

    android:layout_alignEnd=”@+id/textView”

    android:layout_marginTop=”42dp”

    android:layout_alignLeft=”@+id/imageView”

    android:layout_alignStart=”@+id/imageView” />

    < ImageView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/imageView”

    android:src=”@drawable/abc”

    android:layout_below=”@+id/textView”

    android:layout_centerHorizontal=”true” />

    < Button

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”load”

    android:id=”@+id/button2″

    android:layout_alignTop=”@+id/button”

    android:layout_alignRight=”@+id/editText”

    android:layout_alignEnd=”@+id/editText” />

    < TextView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:text=”Read”

    android:id=”@+id/textView2″

    android:layout_below=”@+id/editText”

    android:layout_toLeftOf=”@+id/button2″

    android:layout_toStartOf=”@+id/button2″

    android:textColor=”#ff5bff1f”

    android:textSize=”25dp” />

    < /RelativeLayout>

    Must Read About Android Tutorial.

    MainActivity.java.

    package tutorials.ducatindia.myapplication;

    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.EditText;

    import android.widget.TextView;

    import android.widget.Toast;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    	public class MainActivity extends Activity  {
    	   Button b1,b2;
    	TextView tv;
    	EditText ed1;
    
    	   String data;
    	   private String file = "mydata";
    
    	   @Override
    	   protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_main);
    
    	      b1=(Button)findViewById(R.id.button);
    	      b2=(Button)findViewById(R.id.button2);
    
    	      ed1=(EditText)findViewById(R.id.editText);
    	      tv=(TextView)findViewById(R.id.textView2);
    	      b1.setOnClickListener(new View.OnClickListener() {
    
    	         @Override
    	         public void onClick(View v) {
    	            data=ed1.getText().toString();
    	            try {
    	FileOutputStreamfOut = openFileOutput(file,MODE_WORLD_READABLE);
    	fOut.write(data.getBytes());
    	fOut.close();
    	Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
    	            }
    	            catch (Exception e) {
    	               // TODO Auto-generated catch block
    	e.printStackTrace();
    	            }
    	         }
    	      });
    
    	      b2.setOnClickListener(new View.OnClickListener() {
    
    	         @Override
    	         public void onClick(View v) {
    	            try {
    	FileInputStream fin = openFileInput(file);
    	               int c;
    	               String temp="";
    	while( (c = fin.read()) != -1){
    	                  temp = temp + Character.toString((char)c);
    	               }
    	tv.setText(temp);
    	Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
    	            }
    	catch(Exception e){
    	            }
    
    	   Button b1,b2;
    	TextView tv;
    	EditText ed1;
    
    	   String data;
    	   private String file = "mydata";
    
    	   @Override
    	   protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_main);
    
    	      b1=(Button)findViewById(R.id.button);
    	      b2=(Button)findViewById(R.id.button2);
    
    	      ed1=(EditText)findViewById(R.id.editText);
    	      tv=(TextView)findViewById(R.id.textView2);
    	      b1.setOnClickListener(new View.OnClickListener() {
    
    	         @Override
    	         public void onClick(View v) {
    	            data=ed1.getText().toString();
    	            try {
    	FileOutputStreamfOut = openFileOutput(file,MODE_WORLD_READABLE);
    	fOut.write(data.getBytes());
    	fOut.close();
    	Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
    	            }
    	            catch (Exception e) {
    	               // TODO Auto-generated catch block
    	e.printStackTrace();
    	            }
    	         }
    	      });
    
    	      b2.setOnClickListener(new View.OnClickListener() {
    
    	         @Override
    	         public void onClick(View v) {
    	            try {
    	FileInputStream fin = openFileInput(file);
    	               int c;
    	               String temp="";
    	while( (c = fin.read()) != -1){
    	                  temp = temp + Character.toString((char)c);
    	               }
    	tv.setText(temp);
    	Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
    	            }
    	catch(Exception e){
    	            }
    	         }
    	      });
    	   }
    	}
    	   Button b1,b2;
    	TextView tv;
    	EditText ed1;
    
    	   String data;
    	   private String file = "mydata";
    
    	   @Override
    	   protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_main);
    
    	      b1=(Button)findViewById(R.id.button);
    	      b2=(Button)findViewById(R.id.button2);
    
    	      ed1=(EditText)findViewById(R.id.editText);
    	      tv=(TextView)findViewById(R.id.textView2);
    	      b1.setOnClickListener(new View.OnClickListener() {
    
    	         @Override
    	         public void onClick(View v) {
    	            data=ed1.getText().toString();
    	            try {
    	FileOutputStreamfOut = openFileOutput(file,MODE_WORLD_READABLE);
    	fOut.write(data.getBytes());
    	fOut.close();
    	Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
    	            }
    	            catch (Exception e) {
    	               // TODO Auto-generated catch block
    	e.printStackTrace();
    	            }
    	         }
    	      });
    
    	      b2.setOnClickListener(new View.OnClickListener() {
    
    	         @Override
    	         public void onClick(View v) {
    	            try {
    	FileInputStream fin = openFileInput(file);
    	               int c;
    	               String temp="";
    	while( (c = fin.read()) != -1){
    	                  temp = temp + Character.toString((char)c);
    	               }
    	tv.setText(temp);
    	Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
    	            }
    	catch(Exception e){
    	            }
    	         }
    	      });
    	   }
    	}
    	         }
    	      });
    	   }
    	}
    	
    Output:
    Android Internal Storage

    Enroll Yourself in Live Classes For Android training in delhi.

    Copyright 1999- Ducat Creative, All rights reserved.