Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

External Storage


Like internal storage, we are able to save or read data from the device external memory such as sdcard. 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. <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.     <EditText
  7.         android:id=“@+id/editText1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentRight=“true”
  11.         android:layout_alignParentTop=“true”
  12.         android:layout_marginRight=“20dp”
  13.         android:layout_marginTop=“24dp”
  14.         android:ems=“10” >
  15.         <requestFocus />
  16.     </EditText>
  17.     <EditText
  18.         android:id=“@+id/editText2”
  19.         android:layout_width=“wrap_content”
  20.         android:layout_height=“wrap_content”
  21.         android:layout_alignRight=“@+id/editText1”
  22.         android:layout_below=“@+id/editText1”
  23.         android:layout_marginTop=“24dp”
  24.         android:ems=“10” />
  25.     <TextView
  26.         android:id=“@+id/textView1”
  27.         android:layout_width=“wrap_content”
  28.         android:layout_height=“wrap_content”
  29.         android:layout_alignBaseline=“@+id/editText1”
  30.         android:layout_alignBottom=“@+id/editText1”
  31.         android:layout_alignParentLeft=“true”
  32.         android:text=“File Name:” />
  33.     <TextView
  34.         android:id=“@+id/textView2”
  35.         android:layout_width=“wrap_content”
  36.         android:layout_height=“wrap_content”
  37.         android:layout_alignBaseline=“@+id/editText2”
  38.         android:layout_alignBottom=“@+id/editText2”
  39.         android:layout_alignParentLeft=“true”
  40.         android:text=“Data:” />
  41.     <Button
  42.         android:id=“@+id/button1”
  43.         android:layout_width=“wrap_content”
  44.         android:layout_height=“wrap_content”
  45.         android:layout_alignLeft=“@+id/editText2”
  46.         android:layout_below=“@+id/editText2”
  47.         android:layout_marginLeft=“70dp”
  48.         android:layout_marginTop=“16dp”
  49.         android:text=“save” />
  50.     <Button
  51.         android:id=“@+id/button2”
  52.         android:layout_width=“wrap_content”
  53.         android:layout_height=“wrap_content”
  54.         android:layout_alignBaseline=“@+id/button1”
  55.         android:layout_alignBottom=“@+id/button1”
  56.         android:layout_toRightOf=“@+id/button1”
  57.         android:text=“read” />
  58. </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
  3. xmlns:android=“http://schemas.android.com/apk/res/android”
  4.     package=“com.example.externalstorage”
  5.     android:versionCode=“1”
  6.     android:versionName=“1.0” >
  7.     <uses-sdk
  8.         android:minSdkVersion=“8”
  9.         android:targetSdkVersion=“16” />
  10. <uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE”/>
  11.     <application
  12.         android:allowBackup=“true”
  13.         android:icon=“@drawable/ic_launcher”
  14.         android:label=“@string/app_name”
  15.         android:theme=“@style/AppTheme” >
  16.         <activity
  17.             android:name=“com.example.externalstorage.MainActivity”
  18.             android:label=“@string/app_name” >
  19.             <intent-filter>
  20.                 <action android:name=“android.intent.action.MAIN” />
  21.                 <category android:name=“android.intent.category.LAUNCHER” />
  22.             </intent-filter>
  23.         </activity>
  24.     </application>
  25. </manifest>

Activity class

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

File: MainActivity.java
  1. package com.example.externalstorage;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStreamWriter;
  10. import android.os.Bundle;
  11. import android.app.Activity;
  12. import android.content.Context;
  13. import android.view.Menu;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.Toast;
  19. public class MainActivity extends Activity {
  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.         editTextFileName=(EditText)findViewById(R.id.editText1);
  27.         editTextData=(EditText)findViewById(R.id.editText2);
  28.         saveButton=(Button)findViewById(R.id.button1);
  29.         readButton=(Button)findViewById(R.id.button2);
  30.         //Performing action on save button
  31.         saveButton.setOnClickListener(new OnClickListener(){
  32.             @Override
  33.             public void onClick(View arg0) {
  34.                 String filename=editTextFileName.getText().toString();
  35.                 String data=editTextData.getText().toString();
  36.                 FileOutputStream fos;
  37.                    try {
  38.                        File myFile = new File(“/sdcard/”+filename);
  39.                         myFile.createNewFile();
  40.                         FileOutputStream fOut = new
  41. FileOutputStream(myFile);
  42.                         OutputStreamWriter myOutWriter = new
  43. OutputStreamWriter(fOut);
  44.                         myOutWriter.append(data);
  45.                         myOutWriter.close();
  46.                         fOut.close();
  47.             Toast.makeText(getApplicationContext(),filename + “
  48. saved”,Toast.LENGTH_LONG).show();
  49.                    } catch (FileNotFoundException e) {e.printStackTrace();}
  50.                    catch (IOException e) {e.printStackTrace();}
  51.             }
  52.         });
  53.         //Performing action on Read Button
  54.         readButton.setOnClickListener(new OnClickListener(){
  55.             @Override
  56.             public void onClick(View arg0) {
  57.                 String filename=editTextFileName.getText().toString();
  58.                 StringBuffer stringBuffer = new StringBuffer();
  59.                 String aDataRow = “”;
  60.                 String aBuffer = “”;
  61.                 try {
  62.                     File myFile = new File(“/sdcard/”+filename);
  63.                     FileInputStream fIn = new FileInputStream(myFile);
  64.                     BufferedReader myReader = new BufferedReader(
  65.                             new InputStreamReader(fIn));
  66.                     while ((aDataRow = myReader.readLine()) != null) {
  67.                         aBuffer += aDataRow + “\n”;
  68.                     }
  69.                     myReader.close();
  70.                 } catch (IOException e) {
  71.                     e.printStackTrace();
  72.                 }
  73.                 Toast.makeText(getApplicationContext
  74. (),aBuffer,Toast.LENGTH_LONG).show();
  75.             }
  76.         });
  77.     }
  78.     @Override
  79.     public boolean onCreateOptionsMenu(Menu menu) {
  80.         // Inflate the menu; this adds items to the action bar if it is present.
  81.         getMenuInflater().inflate(R.menu.activity_main, menu);
  82.         return true;
  83.     }
  84. }

Comments

comments