1. 程式人生 > >徹底理解android中的內部儲存與外部儲存

徹底理解android中的內部儲存與外部儲存

我們先來考慮這樣一個問題:

開啟手機設定,選擇應用管理,選擇任意一個App,然後你會看到兩個按鈕,一個是清除快取,另一個是清除資料,那麼當我們點選清除快取的時候清除的是哪裡的資料?當我們點選清除資料的時候又是清除的哪裡的資料?讀完本文相信你會有答案。

在android開發中我們常常聽到這樣幾個概念,記憶體,內部儲存,外部儲存,很多人常常將這三個東西搞混,那麼我們今天就先來詳細說說這三個東西是怎麼回事?

記憶體,我們在英文中稱作memory,內部儲存,我們稱為InternalStorage,外部儲存我們稱為ExternalStorage,這在英文中本不會產生歧義,但是當我們翻譯為中文之後,前兩個都簡稱為記憶體,於是,混了。

那麼究竟什麼是內部儲存什麼是外部儲存呢?

首先我們開啟DDMS,有一個File Explorer,如下:

這裡有三個資料夾需要我們重視,一個是data,一個是mnt,一個是storage,我們下面就詳細說說這三個資料夾。

1.內部儲存

data資料夾就是我們常說的內部儲存,當我們開啟data資料夾之後(沒有root的手機不能開啟該資料夾),裡邊有兩個資料夾值得我們關注,如下:

一個資料夾是app資料夾,還有一個資料夾就是data資料夾,app資料夾裡存放著我們所有安裝的app的apk檔案,其實,當我們除錯一個app的時候,可以看到控制檯輸出的內容,有一項是uploading …..就是上傳我們的apk到這個資料夾,上傳成功之後才開始安裝。另一個重要的資料夾就是data檔案夾了,這個資料夾裡邊都是一些包名,開啟這些包名之後我們會看到這樣的一些檔案:

1.data/data/包名/shared_prefs
2.data/data/包名/databases
3.data/data/包名/files

4.data/data/包名/cache

如果開啟過data檔案,應該都知道這些資料夾是幹什麼用的,我們在使用sharedPreferenced的時候,將資料持久化儲存於本地,其實就是存在這個檔案中的xml檔案裡,我們App裡邊的資料庫檔案就儲存於databases資料夾中,還有我們的普通資料儲存在files中,快取檔案儲存在cache資料夾中,儲存在這裡的檔案我們都稱之為內部儲存。

2.外部儲存

外部儲存才是我們平時操作最多的,外部儲存一般就是我們上面看到的storage資料夾,當然也有可能是mnt資料夾,這個不同廠家有可能不一樣。

一般來說,在storage資料夾中有一個sdcard資料夾,這個資料夾中的檔案又分為兩類,一類是公有目錄,還有一類是私有目錄,其中的公有目錄有九大類,比如DCIM、DOWNLOAD等這種系統為我們建立的資料夾,私有目錄就是Android這個資料夾,這個資料夾開啟之後裡邊有一個data資料夾,開啟這個data資料夾,裡邊有許多包名組成的資料夾。

說到這裡,我想大家應該已經可以分清楚什麼是內部儲存什麼是外部儲存了吧?好,分清楚之後我們就要看看怎麼來操作內部儲存和外部儲存了。

3.操作儲存空間

首先,經過上面的分析,大家已經明白了,什麼是內部儲存,什麼是外部儲存,以及這兩種儲存方式分別儲存在什麼位置,一般來說,我們不會自己去操作內部儲存空間,沒有root許可權的話,我們也沒法操作內部儲存空間,事實上內部儲存主要是由系統來維護的。不過在程式碼中我們是可以訪問到這個資料夾的。由於內部儲存空間有限,在開發中我們一般都是操作外部儲存空間,Google官方建議我們App的資料應該儲存在外部儲存的私有目錄中該App的包名下,這樣當用戶解除安裝掉App之後,相關的資料會一併刪除,如果你直接在/storage/sdcard目錄下建立了一個應用的資料夾,那麼當你刪除應用的時候,這個資料夾就不會被刪除。

經過以上的介紹,我們可以總結出下面一個表格:

一目瞭然,什麼是內部儲存,什麼是外部儲存。

如果按照路徑的特徵,我們又可以將檔案儲存的路徑分為兩大類,一類是路徑中含有包名的,一類是路徑中不含有包名的,含有包名的路徑,因為和某個App有關,所以對這些資料夾的訪問都是呼叫Context裡邊的方法,而不含有包名的路徑,和某一個App無關,我們可以通過Environment中的方法來訪問。如下圖:

大家看到,有包名的路徑我們都是呼叫Context中的方法來獲得,沒有包名的路徑,我們直接呼叫Environment中的方法獲得,那麼其中有兩個方法需要傳入一個String型別的引數,這個引數我們使用了Environment中的常量,引數的意思是我們要訪問這個路徑下的哪個資料夾,比如getExternalFilesDir方法,我們看看它的原始碼:

/**
*
* @param type The type of files directory to return.  May be null for
* the root of the files directory or one of
* the following Environment constants for a subdirectory:
* {@link android.os.Environment#DIRECTORY_MUSIC},
* {@link android.os.Environment#DIRECTORY_PODCASTS},
* {@link android.os.Environment#DIRECTORY_RINGTONES},
* {@link android.os.Environment#DIRECTORY_ALARMS},
* {@link android.os.Environment#DIRECTORY_NOTIFICATIONS},
* {@link android.os.Environment#DIRECTORY_PICTURES}, or
* {@link android.os.Environment#DIRECTORY_MOVIES}.
*
* @return The path of the directory holding application files
* on external storage.  Returns null if external storage is not currently
* mounted so it could not ensure the path exists; you will need to call
* this method again when it is available.
*
* @see #getFilesDir
* @see android.os.Environment#getExternalStoragePublicDirectory
*/
@Nullable
public abstract File getExternalFilesDir(@Nullable String type);

它的註釋非常多,我這裡只列出其中一部分,我們看到,我們可以訪問files資料夾下的Music資料夾、Movies資料夾等等好幾種。

說到這裡,我想大家對內部儲存、外部儲存該有了一個清晰的認識了吧。我們在開發中,不建議往內部儲存中寫太多的資料,畢竟空間有限。外部儲存在使用的時候最好能夠將檔案存放在私有目錄下,這樣有利於系統維護,也避免使用者的反感。

現在我們再來看看我們一開始提出的問題,當我們點選清除資料的時候清除的是哪裡的資料呢?毫無疑問,當然是內部儲存目錄中相應的files和cache資料夾中的檔案和外部儲存中相應的files和cache資料夾中的檔案,至於這些資料夾的路徑我想你應該已經明白了。

好了,最後再送給大家一個檔案操作工具類:

public class SDCardHelper {
 
     // 判斷SD卡是否被掛載
     public static boolean isSDCardMounted() {
         // return Environment.getExternalStorageState().equals("mounted");
         return Environment.getExternalStorageState().equals(
         Environment.MEDIA_MOUNTED);
    }
 
    // 獲取SD卡的根目錄
    public static String getSDCardBaseDir() {
         if (isSDCardMounted()) {
               return Environment.getExternalStorageDirectory().getAbsolutePath();
         }
         return null;
    }
 
    // 獲取SD卡的完整空間大小,返回MB
    public static long getSDCardSize() {
         if (isSDCardMounted()) {
              StatFs fs = new StatFs(getSDCardBaseDir());
              long count = fs.getBlockCountLong();
              long size = fs.getBlockSizeLong();
              return count * size / 1024 / 1024;
         }
         return 0;
    }
 
    // 獲取SD卡的剩餘空間大小
    public static long getSDCardFreeSize() {
         if (isSDCardMounted()) {
               StatFs fs = new StatFs(getSDCardBaseDir());
               long count = fs.getFreeBlocksLong();
               long size = fs.getBlockSizeLong();
               return count * size / 1024 / 1024;
         }
         return 0;
    }
 
    // 獲取SD卡的可用空間大小
    public static long getSDCardAvailableSize() {
         if (isSDCardMounted()) {
               StatFs fs = new StatFs(getSDCardBaseDir());
               long count = fs.getAvailableBlocksLong();
               long size = fs.getBlockSizeLong();
               return count * size / 1024 / 1024;
         }
         return 0;
    }
 
    // 往SD卡的公有目錄下儲存檔案
    public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName) {
         BufferedOutputStream bos = null;
         if (isSDCardMounted()) {
               File file = Environment.getExternalStoragePublicDirectory(type);
               try {
                    bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
                    bos.write(data);
                    bos.flush();
                    return true;
               } catch (Exception e) {
                    e.printStackTrace();
               } finally {
                    try {
                          bos.close();
                    } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                    }
               }
          }
          return false;
     }
 
     // 往SD卡的自定義目錄下儲存檔案
     public static boolean saveFileToSDCardCustomDir(byte[] data, String dir, String fileName) {
          BufferedOutputStream bos = null;
          if (isSDCardMounted()) {
                File file = new File(getSDCardBaseDir() + File.separator + dir);
                if (!file.exists()) {
                      file.mkdirs();// 遞迴建立自定義目錄
                }
                try {
                      bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
                      bos.write(data);
                      bos.flush();
                      return true;
                } catch (Exception e) {
                      e.printStackTrace();
                } finally {
                      try {
                            bos.close();
                      } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                      }
                }
           }
           return false;
     }
 
     // 往SD卡的私有Files目錄下儲存檔案
     public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String fileName, Context context) {
         BufferedOutputStream bos = null;
         if (isSDCardMounted()) {
               File file = context.getExternalFilesDir(type);
               try {
                      bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
                      bos.write(data);
                      bos.flush();
                      return true;
               } catch (Exception e) {
                      e.printStackTrace();
               } finally {
                      try {
                            bos.close();
                      } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                      }
               }
          }
          return false;
     }
 
     // 往SD卡的私有Cache目錄下儲存檔案
     public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context) {
          BufferedOutputStream bos = null;
          if (isSDCardMounted()) {
                File file = context.getExternalCacheDir();
                try {
                      bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
                      bos.write(data);
                      bos.flush();
                      return true;
                } catch (Exception e) {
                      e.printStackTrace();
                } finally {
                      try {
                            bos.close();
                      } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                      }
               }
          }
          return false;
     }
 
     // 儲存bitmap圖片到SDCard的私有Cache目錄
     public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap, String fileName, Context context) {
          if (isSDCardMounted()) {
                BufferedOutputStream bos = null;
                // 獲取私有的Cache快取目錄
                File file = context.getExternalCacheDir();
 
                try {
                       bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
                       if (fileName != null && (fileName.contains(".png") || fileName.contains(".PNG"))) {
                              bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
                       } else {
                              bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                       }
                       bos.flush();
                } catch (Exception e) {
                       e.printStackTrace();
                } finally {
                       if (bos != null) {
                            try {
                                 bos.close();
                            } catch (IOException e) {
                                 e.printStackTrace();
                            }
                       }
                 }
                 return true;
          } else {
                return false;
          }
     }
 
     // 從SD卡獲取檔案
     public static byte[] loadFileFromSDCard(String fileDir) {
          BufferedInputStream bis = null;
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
          try {
                bis = new BufferedInputStream(new FileInputStream(new File(fileDir)));
                byte[] buffer = new byte[8 * 1024];
                int c = 0;
                while ((c = bis.read(buffer)) != -1) {
                     baos.write(buffer, 0, c);
                     baos.flush();
                }
                return baos.toByteArray();
          } catch (Exception e) {
                e.printStackTrace();
          } finally {
                try {
                     baos.close();
                     bis.close();
                } catch (IOException e) {
                     e.printStackTrace();
                }
          }
          return null;
     }
 
     // 從SDCard中尋找指定目錄下的檔案,返回Bitmap
     public Bitmap loadBitmapFromSDCard(String filePath) {
          byte[] data = loadFileFromSDCard(filePath);
          if (data != null) {
               Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
               if (bm != null) {
                     return bm;
               }
          }
          return null;
     }
 
     // 獲取SD卡公有目錄的路徑
     public static String getSDCardPublicDir(String type) {
          return Environment.getExternalStoragePublicDirectory(type).toString();
     }
 
     // 獲取SD卡私有Cache目錄的路徑
     public static String getSDCardPrivateCacheDir(Context context) {
          return context.getExternalCacheDir().getAbsolutePath();
     }
 
     // 獲取SD卡私有Files目錄的路徑
     public static String getSDCardPrivateFilesDir(Context context, String type) {
          return context.getExternalFilesDir(type).getAbsolutePath();
     }
 
     public static boolean isFileExist(String filePath) {
          File file = new File(filePath);
          return file.isFile();
     }
 
     // 從sdcard中刪除檔案
     public static boolean removeFileFromSDCard(String filePath) {
          File file = new File(filePath);
          if (file.exists()) {
               try {
                     file.delete();
                     return true;
               } catch (Exception e) {
                     return false;
               }
          } else {
               return false;
          }
     }
}