The previous section describes how to store files to internal devices. Sometimes, you need to store files to external storage devices, such as SD cards. Because the SD card has a larger storage space and can easily share these files with other users.
Use the example in the previous section to save the text you entered 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 when the activity is first created. */@Overridepublic 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 = newOutputStreamWriter(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 above Code, use the getexternalstoragedirectory () method to obtain the path of the SD card. Generally, "/sdcard" is returned on the real machine, and "/mnt/sdcard" is returned on the simulator ". However, do not try to write the path of the SD card because the mobile phone manufacturer may specify a path for the SD card. Therefore, use the getexternalstoragedirectory () method to obtain the path of the SD card.
Create a folder named myfiles. Finally, save the file in this folder.
To load files 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 that has been // read---textBox.setText(s);Toast.makeText(getBaseContext(),"File loaded successfully!",Toast.LENGTH_SHORT).show();}catch (IOException ioe) {ioe.printStackTrace();}}
Please note that if you want to write files to the SD card, you must add related permissions to androidmanifest. xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.manoel.Files" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".FilesActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
Run the above Code to view the SD card: