Tuesday, March 16, 2021

Android Internal Storage

 

Android Internal Storage Example

We are able to save or read data from the device internal memory. FileInputStream and FileOutputStream classes are used to read and write data into the file.

Here, we are going to read and write data to the internal storage of the device.


Example of reading and writing data to the android internal storage

activity_main.xml

Drag the 2 edittexts, 2 textviews and 2 buttons from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  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.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginRight="20dp"  
  14.         android:layout_marginTop="24dp"  
  15.         android:ems="10" >  
  16.   
  17.         <requestFocus />  
  18.     </EditText>  
  19.   
  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.   
  29.     <TextView  
  30.         android:id="@+id/textView1"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_alignBaseline="@+id/editText1"  
  34.         android:layout_alignBottom="@+id/editText1"  
  35.         android:layout_alignParentLeft="true"  
  36.         android:text="File Name:" />  
  37.   
  38.     <TextView  
  39.         android:id="@+id/textView2"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:layout_alignBaseline="@+id/editText2"  
  43.         android:layout_alignBottom="@+id/editText2"  
  44.         android:layout_alignParentLeft="true"  
  45.         android:text="Data:" />  
  46.   
  47.     <Button  
  48.         android:id="@+id/button1"  
  49.         android:layout_width="wrap_content"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_alignLeft="@+id/editText2"  
  52.         android:layout_below="@+id/editText2"  
  53.         android:layout_marginLeft="70dp"  
  54.         android:layout_marginTop="16dp"  
  55.         android:text="save" />  
  56.   
  57.     <Button  
  58.         android:id="@+id/button2"  
  59.         android:layout_width="wrap_content"  
  60.         android:layout_height="wrap_content"  
  61.         android:layout_alignBaseline="@+id/button1"  
  62.         android:layout_alignBottom="@+id/button1"  
  63.         android:layout_toRightOf="@+id/button1"  
  64.         android:text="read" />  
  65.   
  66. </RelativeLayout>  


Activity class

Let's write the code to write and read data from the internal storage.

File: MainActivity.java
  1. package example.javatpoint.com.internalstorage;  
  2.   
  3. import android.content.Context;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.Toast;  
  10.   
  11. import java.io.BufferedReader;  
  12. import java.io.FileNotFoundException;  
  13. import java.io.FileOutputStream;  
  14. import java.io.IOException;  
  15. import java.io.InputStreamReader;  
  16.   
  17. public class MainActivity extends AppCompatActivity {  
  18.     EditText editTextFileName,editTextData;  
  19.     Button saveButton,readButton;  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         editTextFileName=findViewById(R.id.editText1);  
  26.         editTextData=findViewById(R.id.editText2);  
  27.         saveButton=findViewById(R.id.button1);  
  28.         readButton=findViewById(R.id.button2);  
  29.   
  30.         //Performing Action on Read Button  
  31.         saveButton.setOnClickListener(new View.OnClickListener(){  
  32.   
  33.             @Override  
  34.             public void onClick(View arg0) {  
  35.                 String filename=editTextFileName.getText().toString();  
  36.                 String data=editTextData.getText().toString();  
  37.   
  38.                 FileOutputStream fos;  
  39.                 try {  
  40.                     fos = openFileOutput(filename, Context.MODE_PRIVATE);  
  41.                     //default mode is PRIVATE, can be APPEND etc.  
  42.                     fos.write(data.getBytes());  
  43.                     fos.close();  
  44.   
  45.                     Toast.makeText(getApplicationContext(),filename + " saved",  
  46.                             Toast.LENGTH_LONG).show();  
  47.   
  48.   
  49.                 } catch (FileNotFoundException e) {e.printStackTrace();}  
  50.                 catch (IOException e) {e.printStackTrace();}  
  51.   
  52.             }  
  53.   
  54.         });  
  55.   
  56.         //Performing Action on Read Button  
  57.         readButton.setOnClickListener(new View.OnClickListener(){  
  58.   
  59.             @Override  
  60.             public void onClick(View arg0) {  
  61.                 String filename=editTextFileName.getText().toString();  
  62.                 StringBuffer stringBuffer = new StringBuffer();  
  63.                 try {  
  64.                     //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader  
  65.                     BufferedReader inputReader = new BufferedReader(new InputStreamReader(  
  66.                             openFileInput(filename)));  
  67.                     String inputString;  
  68.                     //Reading data line by line and storing it into the stringbuffer  
  69.                     while ((inputString = inputReader.readLine()) != null) {  
  70.                         stringBuffer.append(inputString + "\n");  
  71.                     }  
  72.   
  73.                 } catch (IOException e) {  
  74.                     e.printStackTrace();  
  75.                 }  
  76.                 //Displaying data on the toast  
  77.             Toast.makeText(getApplicationContext(),stringBuffer.toString(),Toast.LENGTH_LONG).show();  
  78.   
  79.             }  
  80.   
  81.         });  
  82.     }  
  83. }  

Output:

android internal storage 1 android internal storage 2 android internal storage 3 android internal storage 4 android internal storage 5

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile