綁定帳號登入

Android 台灣中文網

Android 台灣中文網 標籤 storage 相關日誌

tag 標籤: storage

相關日誌

分享 APP storage 讀寫檔案
jianrupan 2013-10-11 12:35
http://android-deve.blogspot.tw/ 1. 存在手機還是 SDcard Android 可以將檔案儲存在 手機上的記憶體 (Internal Storage) ,或是 外部儲存媒體 SDcard(External Storage) 。 android.content.ContextWrapper 類別中的方法, this 是指該 Activity 。 Context.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 ; 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 ; 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(); }
個人分類: 軟體應用|3334 次閱讀|0 個評論