The previous section describes how to store files to an internal device. Sometimes, you need to store files to an external storage device, such as an SD card. Because SD cards have greater storage space, they can also be easily shared with other users.
Use the example in the previous section to save the text entered by the user on the SD card and modify the OnClick () event. As follows:
public class Filesactivity extends activity {edittext TextBox;
static final int read_block_size = 100; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
TextBox = (edittext) Findviewbyid (R.ID.TXTTEXT1);
InputStream is = This.getresources (). Openrawresource (R.raw.textfile);
BufferedReader br = new BufferedReader (new InputStreamReader (IS));
String str = NULL;
try {while (str = Br.readline ())!= null) {Toast.maketext (Getbasecontext ()),
STR, toast.length_short). Show ();
} is.close ();
Br.close ();
catch (IOException e) {e.printstacktrace ();
} public void Onclicksave (view view) { String str = Textbox.gettext (). toString ();
try {//---SD card Storage---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);
/* FileOutputStream fout = openfileoutput ("Textfile.txt",
mode_world_readable);
* * OutputStreamWriter OSW = new OutputStreamWriter (fout);
---Write the string to the file---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 (); }
}
}
In the code above, use the getExternalStorageDirectory () method to get the path to the SD card. Normally, return "/sdcard" above the real machine and return "/mnt/sdcard" on the emulator. However, do not attempt to write the path of the dead SD card, because the handset manufacturer may go to the SD card to specify a path. Therefore, be sure to use the getExternalStorageDirectory () method to obtain the path to the SD card.
Then, create a myfiles folder. Finally, save the file in this folder.
To load a file from an external device, modify the Onclickload () method:
public void Onclickload (view view) {try {//---SD Storage---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);
/* FileInputStream fIn = openfileinput ("Textfile.txt");
InputStreamReader ISR = new InputStreamReader (fIn);
* * char[] InputBuffer = new Char[read_block_size];
String s = "";
int charread;
while ((Charread = Isr.read (inputbuffer)) >0) {//---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 this has been//Read---textbox.settext (s)
; Toast.maketext (Getbasecontext (), "File loaded successfully!", Toast.length_sho
RT). Show ();
catch (IOException IoE) {ioe.printstacktrace (); }
}