CS453 | mobile programming
  • outline
  • projects
  • syllabus
  • links

Android File Writing to External Storage

CHANGE from Internal Storage=

  • grab "directory handle" to SD card =Environment.getExtranalStorageDirectory();
  • when opening file ---> specify the directory of where the file is on the SD card!!  

Step 0: Create unique directory on SD Card to store your file in using Environment.getExternalSgtorageDirectory() ----see below

 

//Inside your Activity class

File sdCard = Environment.getExternalStorageDirectory();

FIle directory = new File(sdCard.getAbsolutePath() + "/MyFiles" ); //here MyFiles is sub-directory unique to you
directory.mkdirs(); //create the directory.

 

 

Step 1: Use java.io.FileOutputStream class to open file, use Activities openFileOutput(file ) method from Activity class, where file = new File(directory, "filename") where directory created in step 1

 

//Inside your Activity class
//**CODE STEP 0

File file = new File(directory, "textfile.txt"); //creates file in specified directory which is on SD card


FileOutputStream fOut = new FileOutputStream(file);

Step 2: OPTIONAL convert your FileOutputStream instance of step 1 to a easier to use java.io class //NOTE Same as Internal Storage

OutputStreamWriter osw = new OutputSteramWriter (fOut);

 

Step 3: Write , Flush and Close when done //NOTE Same as Internal Storage

osw.write("string");

osw.flush();

osw.close();

 

 

 

Android File Reading from External Storage

Step 0: Build the directory name of where file is located on SD Card using Environment.getExternalSgtorageDirectory() ----see below

 

//Inside your Activity class

File sdCard = Environment.getExternalStorageDirectory();

File directory = new File(sdCard.getAbsolutePath() + "/MyFiles" ); //here MyFiles is sub-directory unique to you

 

Step 1: Use java.io.FileInputStream class to open file using openFileInput(*) method from Activity class

//inside Activity class
//****code from Step 0 ****

File file = new File(directory, "textfile.txt");
FileInputStream fIn = openFileInput(file);

 

Other Modes:           MODE_APPEND, MODE_WORLD_WRITEABLE

Step 2: OPTIONAL convert your FileOutputStream instance of step 1 to a easier to use java.io class //NOTE Same as Internal Storage

InputStreamReader isw = new InputStreamReader (fIn);

 

Step 3: Read and Close when done //NOTE Same as Internal Storage

int charRead = isw.read();

iws.close();

 

 

 

EXAMPLE: writing and reading to file in External Storage

main.xml App's Activity layout   

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"save and read from internal file
 android:layout_height="fill_parent"
 android:orientation="vertical" >
  <TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="Please enter some text" />
  <EditText    android:id="@+id/txtText1"    android:layout_width="fill_parent"    android:layout_height="wrap_content" />   <Button    android:id="@+id/btnSave"    android:text="Save"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:onClick="onClickSave" />
  <Button    android:id="@+id/btnLoad"    android:text="Load"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:onClick="onClickLoad" /> </LinearLayout>

 

App's Activity    

public class FilesActivity extends Activity {
 EditText textBox;
 static final int READ_BLOCK_SIZE = 100;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textBox = (EditText) findViewById(R.id.txtText1); } public void onClickSave(View view) { String str = textBox.getText().toString(); try { //---SD Card Storage--- STEP 1: Open up OutputStream to File in /MyFiles/textfile.txt File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard.getAbsolutePath() + "/MyFiles"); directory.mkdirs(); File file = new File(directory, "textfile.txt"); FileOutputStream fOut = new FileOutputStream(file);

//STEP 2: Create OutputStreamWriter from FileOutputStream OutputStreamWriter osw = new OutputStreamWriter(fOut); //---write the string to the file--- STEP 3 write, flush, close osw.write(str); osw.flush(); osw.close(); //---display file saved message--- Toast.makeText(getBaseContext(), "File saved successfully!",Toast.LENGTH_SHORT).show(); //---clears the EditText--- textBox.setText(""); } catch (IOException ioe) { ioe.printStackTrace(); } } public void onClickLoad(View view) { try { //---SD Storage--- //STEP 1 Opening to Read from /MyFiles/textfile.txt File sdCard = Environment.getExternalStorageDirectory(); File directory = new File (sdCard.getAbsolutePath() + "/MyFiles"); File file = new File(directory, "textfile.txt"); FileInputStream fIn = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fIn); char[] inputBuffer = new char[READ_BLOCK_SIZE]; String s = ""; int charRead; while ((charRead = isr.read(inputBuffer))>0) //STEP 3: read from file until eof, -1 returned { //---convert the chars to a String--- String readString = String.copyValueOf(inputBuffer, 0,charRead); s += readString;
inputBuffer = new char[READ_BLOCK_SIZE]; } //---set the EditText to the text that has been // read--- textBox.setText(s); Toast.makeText(getBaseContext(), "File loaded successfully!", Toast.LENGTH_SHORT).show(); } catch (IOException ioe) { ioe.printStackTrace(); } } }

 

 

 

 

cs453:mobile programming

  • home
  • outline
  • projects
  • syllabus
  • links