안드로이드 sdcard 파일 읽고 쓰기

SDCard.java

package exam.SDCard;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SDCard extends Activity {
    /** Called when the activity is first created. */
    EditText mEdit;
    String mSdPath;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mEdit = (EditText)findViewById(R.id.edittext);
        findViewById(R.id.test).setOnClickListener(mClickListener);
        findViewById(R.id.save).setOnClickListener(mClickListener);
        findViewById(R.id.load).setOnClickListener(mClickListener);
        
        String ext = Environment.getExternalStorageState();
        if(ext.equals(Environment.MEDIA_MOUNTED)) {
            mSdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        } else {
            mSdPath = Environment.MEDIA_UNMOUNTED;
        }
    }
    
    Button.OnClickListener mClickListener = new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()) {
            case R.id.test :
                String rootdir = Environment.getRootDirectory().getAbsolutePath();
                String datadir = Environment.getDataDirectory().getAbsolutePath();
                String cachedir =
                    Environment.getDownloadCacheDirectory().getAbsolutePath();
                mEdit.setText(String.format("ext = %s\nroot=%s\ndata=%s\ncache=%s", mSdPath, rootdir, datadir, cachedir));
                break;
            case R.id.save :
                File dir = new File(mSdPath + "/dir");
                dir.mkdir();
                File file = new File(mSdPath + "/dir/file.txt");
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    String str = "This file exists in SDcard";
                    fos.write(str.getBytes());
                    fos.close();
                    mEdit.setText("write success");
                }
                catch (FileNotFoundException e) {
                    mEdit.setText("Security Exception");
                }
                catch (Exception e) {
                    mEdit.setText(e.getMessage());
                }
                break;
            case R.id.load :
                try {
                    FileInputStream fis = new FileInputStream(mSdPath + "/dir/file.txt");
                    byte[] data = new byte[fis.available()];
                    while(fis.read(data) != -1) {;}
                    fis.close();
                    mEdit.setText(new String(data));
                } catch (FileNotFoundException e) {
                    mEdit.setText("File Not Found");
                }
                catch (Exception e) {;}
                break;
            }
        }
    };
}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
    <Button 
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
    />
    
    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
    />
    
    <Button
        android:id="@+id/load"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load"
    />
    </LinearLayout>
    
    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    
</LinearLayout>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="exam.SDCard"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SDCard"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

 

Leave a Reply

Your email address will not be published. Required fields are marked *