마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

external sd card 에 파일 저장하기.

0 추천
안녕하세요... 혼자서 안드로이드 공부하면서 app 만들어 보고 있는 초보 개발자입니다.

exteral sd card 에 파일을 저장하고 불러오고 싶은데.. 파일 불러오는 방법을 몰라 이렇게 글 남기네요...

아래 같은경우는  internal sd card 경로인거 같은데 external sd card 경로 컨드롤 하는 방법 아시는 고수님들 답변 부탁드릴께요.. ^^

SavePath = Environment.getExternalStorageDirectory() ;
익명사용자 님이 2013년 5월 15일 질문

1개의 답변

0 추천

http://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage

Try This :

  1. Check External storage device
  2. Write File
private static final String TAG = "MEDIA";
private TextView tv;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv = (TextView) findViewById(R.id.TextView01);
    checkExternalMedia();
    writeToSDFile();
    readRaw();
}

/**
 * Method to check whether external media available and writable. This is
 * adapted from
 * http://developer.android.com/guide/topics/data/data-storage.html
 * #filesExternal
 */

private void checkExternalMedia() {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // Can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // Can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        // Can't read or write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    tv.append("\n\nExternal Media: readable=" + mExternalStorageAvailable
            + " writable=" + mExternalStorageWriteable);
}

/**
 * Method to write ascii text characters to file on SD card. Note that you
 * must add a WRITE_EXTERNAL_STORAGE permission to the manifest file or this
 * method will throw a FileNotFound Exception because you won't have write
 * permission.
 */

private void writeToSDFile() {

    // Find the root of the external storage.
    // See http://developer.android.com/guide/topics/data/data-
    // storage.html#filesExternal

    File root = android.os.Environment.getExternalStorageDirectory();
    tv.append("\nExternal file system root: " + root);

    // See
    // http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File(root.getAbsolutePath() + "/download");
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Hi , How are you");
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG,
                "******* File not found. Did you"
                        + " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
    tv.append("\n\nFile written to " + file);
}

/**
 * Method to read in a text file placed in the res/raw directory of the
 * application. The method reads in all lines of the file sequentially.
 */

private void readRaw() {
    tv.append("\nData read from res/raw/textfile.txt:");
    InputStream is = this.getResources().openRawResource(R.raw.textfile);
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
                                                        // size

    // More efficient (less readable) implementation of above is the
    // composite expression
    /*
     * BufferedReader br = new BufferedReader(new InputStreamReader(
     * this.getResources().openRawResource(R.raw.textfile)), 8192);
     */

    try {
        String test;
        while (true) {
            test = br.readLine();
            // readLine() returns null if no more lines in the file
            if (test == null)
                break;
            tv.append("\n" + "    " + test);
        }
        isr.close();
        is.close();
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    tv.append("\n\nThat is all");
}

 

aucd29 (218,390 포인트) 님이 2013년 5월 15일 답변
...