Tuesday, March 16, 2021

Android External Storage

 

Android External Storage Example

Like internal storage, we are able to save or read data from the device external memory such as sdcard. The FileInputStream and FileOutputStream classes are used to read and write data into the file.


Example of reading and writing data in the android external 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. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.externalstorage.MainActivity">  
  8.   
  9.     <EditText  
  10.         android:id="@+id/editText1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_alignParentRight="true"  
  14.         android:layout_alignParentTop="true"  
  15.         android:layout_marginRight="20dp"  
  16.         android:layout_marginTop="24dp"  
  17.         android:ems="10" >  
  18.   
  19.         <requestFocus />  
  20.     </EditText>  
  21.   
  22.     <EditText  
  23.         android:id="@+id/editText2"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_alignRight="@+id/editText1"  
  27.         android:layout_below="@+id/editText1"  
  28.         android:layout_marginTop="24dp"  
  29.         android:ems="10" />  
  30.   
  31.     <TextView  
  32.         android:id="@+id/textView1"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_alignBaseline="@+id/editText1"  
  36.         android:layout_alignBottom="@+id/editText1"  
  37.         android:layout_alignParentLeft="true"  
  38.         android:text="File Name:" />  
  39.   
  40.     <TextView  
  41.         android:id="@+id/textView2"  
  42.         android:layout_width="wrap_content"  
  43.         android:layout_height="wrap_content"  
  44.         android:layout_alignBaseline="@+id/editText2"  
  45.         android:layout_alignBottom="@+id/editText2"  
  46.         android:layout_alignParentLeft="true"  
  47.         android:text="Data:" />  
  48.   
  49.     <Button  
  50.         android:id="@+id/button1"  
  51.         android:layout_width="wrap_content"  
  52.         android:layout_height="wrap_content"  
  53.         android:layout_alignLeft="@+id/editText2"  
  54.         android:layout_below="@+id/editText2"  
  55.         android:layout_marginLeft="70dp"  
  56.         android:layout_marginTop="16dp"  
  57.         android:text="save" />  
  58.   
  59.     <Button  
  60.         android:id="@+id/button2"  
  61.         android:layout_width="wrap_content"  
  62.         android:layout_height="wrap_content"  
  63.         android:layout_alignBaseline="@+id/button1"  
  64.         android:layout_alignBottom="@+id/button1"  
  65.         android:layout_toRightOf="@+id/button1"  
  66.         android:text="read" />  
  67. </RelativeLayout>  


Provide permission for the external storage

You need to provide the WRITE_EXTERNAL_STORAGE permission.

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
File: Activity_Manifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="example.javatpoint.com.externalstorage">  
  4.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:roundIcon="@mipmap/ic_launcher_round"  
  10.         android:supportsRtl="true"  
  11.         android:theme="@style/AppTheme">  
  12.         <activity android:name=".MainActivity">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.   
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.     </application>  
  20.   
  21. </manifest>  

Activity class

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

File: MainActivity.java
  1. package example.javatpoint.com.externalstorage;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.Toast;  
  9.   
  10. import java.io.BufferedReader;  
  11. import java.io.File;  
  12. import java.io.FileInputStream;  
  13. import java.io.FileNotFoundException;  
  14. import java.io.FileOutputStream;  
  15. import java.io.IOException;  
  16. import java.io.InputStreamReader;  
  17. import java.io.OutputStreamWriter;  
  18.   
  19. public class MainActivity extends AppCompatActivity {  
  20.     EditText editTextFileName,editTextData;  
  21.     Button saveButton,readButton;  
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_main);  
  26.   
  27.         editTextFileName=findViewById(R.id.editText1);  
  28.         editTextData=findViewById(R.id.editText2);  
  29.         saveButton=findViewById(R.id.button1);  
  30.         readButton=findViewById(R.id.button2);  
  31.   
  32.         //Performing action on save button  
  33.         saveButton.setOnClickListener(new View.OnClickListener(){  
  34.   
  35.             @Override  
  36.             public void onClick(View arg0) {  
  37.                 String filename=editTextFileName.getText().toString();  
  38.                 String data=editTextData.getText().toString();  
  39.   
  40.                 FileOutputStream fos;  
  41.                 try {  
  42.                     File myFile = new File("/sdcard/"+filename);  
  43.                     myFile.createNewFile();  
  44.                     FileOutputStream fOut = new FileOutputStream(myFile);  
  45.                     OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);  
  46.                     myOutWriter.append(data);  
  47.                     myOutWriter.close();  
  48.                     fOut.close();  
  49.                     Toast.makeText(getApplicationContext(),filename + "saved",Toast.LENGTH_LONG).show();  
  50.                 } catch (FileNotFoundException e) {e.printStackTrace();}  
  51.                 catch (IOException e) {e.printStackTrace();}  
  52.             }  
  53.         });  
  54.   
  55.         //Performing action on Read Button  
  56.         readButton.setOnClickListener(new View.OnClickListener(){  
  57.             @Override  
  58.             public void onClick(View arg0) {  
  59.                 String filename=editTextFileName.getText().toString();  
  60.                 StringBuffer stringBuffer = new StringBuffer();  
  61.                 String aDataRow = "";  
  62.                 String aBuffer = "";  
  63.                 try {  
  64.                     File myFile = new File("/sdcard/"+filename);  
  65.                     FileInputStream fIn = new FileInputStream(myFile);  
  66.                     BufferedReader myReader = new BufferedReader(  
  67.                             new InputStreamReader(fIn));  
  68.                     while ((aDataRow = myReader.readLine()) != null) {  
  69.                         aBuffer += aDataRow + "\n";  
  70.                     }  
  71.                     myReader.close();  
  72.                 } catch (IOException e) {  
  73.                     e.printStackTrace();  
  74.                 }  
  75.                 Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();  
  76.             }  
  77.         });  
  78.     }  
  79. }  
Android External Storage 1 Android External Storage 2

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