Save to Internal Storage

Description

The following code shows how to save files to Internal Storage.

Example

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;
/*from  w  ww. ja  v a2 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();
        }
    }
}

Note

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.

Note 2

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>





















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS