註冊 登錄
Android 台灣中文網 返回首頁

jianrupan的個人空間 https://apk.tw/?1180935 [收藏] [複製] [分享] [RSS]

日誌

APP storage 讀寫檔案

已有 3333 次閱讀2013-10-11 12:35 |個人分類:軟體應用| APP, storage, 讀寫檔案

http://android-deve.blogspot.tw/

1. 存在手機還是SDcard

Android可以將檔案儲存在手機上的記憶體(Internal Storage),或是外部儲存媒體 SDcard(External Storage)

android.content.ContextWrapper
類別中的方法,this是指該ActivityContext.MODE_PRIVATE是指該檔案為私有的,只能由該App讀寫。另外還有其他指示性的常數,在 Context 抽象類別可以找到以 MODE_ 開頭的常數。

 

External storage

//取得外部儲存媒體的目錄(這裡會是/sdcard)

String path = Environment.getExternalStorageDirectory().getPath();

//檔案路徑,記得要加斜線(這樣/sdcard/filename)

File file = new File(path + "/" + filename);

//讀檔

FileInputStream fin = new FileInputStream(file);

//寫檔

FileOutputStream fout = new FileOutputStream(file);

 

基本上就是使用原本的Java.io裡的類別。這樣檔案會直接儲存在SDcard下,或許你會想要建立一個自己App使用的目錄,使用的方式如下:

//先取得sdcard目錄

String path = Environment.getExternalStorageDirectory().getPath();

//利用File來設定目錄的名稱(myappdir)

File dir = new File(path + "/myappdir");

//先檢查該目錄是否存在

if (!dir.exists()){

    //若不存在則建立它

    dir.mkdir();

}

如此就會有一個 /sdcard/myappdir 的目錄產生。之後你的檔案就可以放在

File file = new File(path + "/myappdir/" + filename);

此外,在使用External storage之前,最好先檢查一下外部媒是否存在及能否讀寫。

//取得外部儲存媒體的狀態

String state = Environment.getExternalStorageState();

//判斷狀態

if (Environment.MEDIA_MOUNTED.equals(state)) {

   Log.d(TAG, "可以讀寫");

} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

   Log.d(TAG, "只可以讀取,無法寫入");

} else {

   Log.d(TAG, "無法讀寫");

}

 

3. 程式碼

實際使用的程式碼寫成方法,方便使用,請依自己的需求修改,如下:

Internal storage

//寫檔

private void writeDataToFile(String filename, String data){

  try {

    FileOutputStream fout = this.openFileOutput(filename, Context.MODE_PRIVATE);

    fout.write(data.getBytes());

    fout.close();

  } catch (FileNotFoundException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

}

 

//讀檔

private String readDataFromFile(String filename){

  String result = null;

  try {

    StringBuilder sb = new StringBuilder();

    FileInputStream fin = this.openFileInput(filename);

    byte[] data = new byte[fin.available()];

    while (fin.read(data) != -1) {

      sb.append(new String(data));

    }

    fin.close();

    result = sb.toString();

  } catch (FileNotFoundException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

return result;

}

 

External storage

//寫檔到sdcard

private void writeToSDcard(String filename, String data){

   //建立自己的目錄

   String path = Environment.getExternalStorageDirectory().getPath();

   File dir = new File(path + "/movietime");

   if (!dir.exists()){

     dir.mkdir();

   }

 

   try {

       File file = new File(path + "/movietime/" + filename);

       FileOutputStream fout = new FileOutputStream(file);

       fout.write(data.getBytes());

       fout.close();

   } catch (FileNotFoundException e) {

       e.printStackTrace();

   } catch (IOException e) {

       e.printStackTrace();

   }

   Log.d(TAG, "Write to SDCARD!");

}

 

//sdcard讀檔

private String readFromSDcard(String filename){

    String path = Environment.getExternalStorageDirectory().getPath();

    File file = new File(path + "/movietime/" + filename);

    StringBuilder sb = new StringBuilder();

    try {

        FileInputStream fin = new FileInputStream(file);

        byte[] data = new byte[fin.available()];

        while (fin.read(data) != -1) {

            sb.append(new String(data));

        }

        fin.close();

    } catch (FileNotFoundException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

    Log.d("TAG", "Read from SDCARD: " + json.toString());

    return sb.toString();

}

 


路過

雞蛋

鮮花

握手

雷人

評論 (0 個評論)

facelist

您需要登錄後才可以評論 登錄 | 註冊