The following code shows how to save files to Internal Storage.
Main layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" 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>
MainActivity.java file
package com.java2s.myapplication3.app; // w ww.j av a 2 s .c o m import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class MainActivity extends Activity { EditText textBox; static final int READ_BLOCK_SIZE = 100; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textBox = (EditText) findViewById(R.id.txtText1); } 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); OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.write(str); osw.flush(); osw.close(); Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show(); textBox.setText(""); } catch (IOException ioe) { ioe.printStackTrace(); } } }
The preceding code uses the getExternalStorageDirectory() method to return the full path to the external storage.
Typically, it should return the "/sdcard" path for a real device, and "/mnt/ sdcard" for an Android emulator.
You should never try to hardcode the path to the SD card, as manufacturers may choose to assign a different path name to the SD card.
In order to write to the external storage, you need to add the WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.java2s.Files" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <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>
The following code shows how to Write and read from Internal Storage.
Main layout xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/internal_storage_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10px" android:text="Enter some text to write on the internal storage, then read back:" /> <EditText android:id="@+id/internal_storage_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10px" /> <Button android:id="@+id/internal_storage_write_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Write" /> <Button android:id="@+id/internal_storage_read_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Read" /> <TextView android:id="@+id/internal_storage_output" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10px" /> </LinearLayout>
Main Activity Java code
// www .ja v a 2 s. co m package com.java2s.myapplication3.app; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class MainActivity extends Activity { private static final String LINE_SEP = System.getProperty("line.separator"); private EditText input; private TextView output; private Button write; private Button read; @Override public void onCreate(final Bundle icicle) { super.onCreate(icicle); this.setContentView(R.layout.activity_main); this.input = (EditText) findViewById(R.id.internal_storage_input); this.output = (TextView) findViewById(R.id.internal_storage_output); this.write = (Button) findViewById(R.id.internal_storage_write_button); this.write.setOnClickListener(new OnClickListener() { public void onClick(final View v) { write(); } }); this.read = (Button) findViewById(R.id.internal_storage_read_button); this.read.setOnClickListener(new OnClickListener() { public void onClick(final View v) { read(); } }); } private void write() { FileOutputStream fos = null; try { // note that there are many modes you can use fos = openFileOutput("test.txt", Context.MODE_PRIVATE); fos.write(input.getText().toString().getBytes()); Toast.makeText(this, "File written", Toast.LENGTH_SHORT).show(); input.setText(""); output.setText(""); } catch (FileNotFoundException e) { Log.e("java2s.com", "File not found", e); } catch (IOException e) { Log.e("java2s.com", "IO problem", e); } finally { try { fos.close(); } catch (IOException e) { // ignore, and take the verbosity punch from Java ;) } } } private void read() { FileInputStream fis = null; Scanner scanner = null; StringBuilder sb = new StringBuilder(); try { fis = openFileInput("test.txt"); // scanner does mean one more object, but it's easier to work with scanner = new Scanner(fis); while (scanner.hasNextLine()) { sb.append(scanner.nextLine() + LINE_SEP); } Toast.makeText(this, "File read", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { Log.e("java2s.com", "File not found", e); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { // ignore, and take the verbosity punch from Java ;) } } if (scanner != null) { scanner.close(); } } output.setText(sb.toString()); } }