1. 程式人生 > >獲取Android應用專屬快取儲存目錄

獲取Android應用專屬快取儲存目錄

獲取Android應用專屬快取儲存目錄

如果你想擺脫快取目錄使用的尷尬:找不到目錄?忘記申請讀寫許可權?害怕汙染使用者儲存空間?……請往下看

SD卡快取目錄

當應用需要將圖片或者檔案快取到SD卡中時要去申請建立目錄,有下面幾種途徑
我們可以通過API呼叫應用專屬目錄:

// /storage/emulated/0/Android/data/app_package_name/files/Pictures
Content.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
// /storage/emulated/0/Android/data/app_package_name/cache
Content.getExternalCacheDir();

上面兩個目錄是專屬於當前app的,當應用被刪除時,上面目錄下的檔案也會清空

記憶體快取目錄

相對於應用的專屬SD卡快取有兩個記憶體快取地址:

Content. getCacheDir(); //  /data/data/app_package_name/cache
Content. getFilesDir(); //  /data/data/app_package_name/files

這兩個目錄中的檔案也會隨著app的刪除而清空

當系統版本大於等於4.4時,對通過上面4個API呼叫得到的目錄進行檔案的讀寫操作不需要申請SD卡的讀寫許可權,所以6.0及以上系統使用時也不需要動態申請讀寫許可權

使用注意事項

  1. 當儲存比較大的檔案時,如圖片等檔案儲存在SD卡對應的目錄下
  2. 應用的記憶體快取目錄只有應用本身能對其進行讀寫操作,外部應用不行,如相機應用 (記憶體目錄讀寫許可權:rwxr-x–x,SD卡快取目錄讀寫許可權:rwxrwx—)
  3. 即使是通過自定義路徑得到的上述目錄,在系統版本大於等於4.4時也不需要申請SD卡讀寫許可權

API使用及方法封裝

/**
 * 獲取應用專屬快取目錄
 * android 4.4及以上系統不需要申請SD卡讀寫許可權
 * 因此也不用考慮6.0系統動態申請SD卡讀寫許可權問題,切隨應用被解除安裝後自動清空 不會汙染使用者儲存空間
 * @param
context 上下文 * @param type 資料夾型別 可以為空,為空則返回API得到的一級目錄 * @return 快取資料夾 如果沒有SD卡或SD卡有問題則返回記憶體快取目錄,否則優先返回SD卡快取目錄 */
public static File getCacheDirectory(Context context,String type) { File appCacheDir = getExternalCacheDirectory(context,type); if (appCacheDir == null){ appCacheDir = getInternalCacheDirectory(context,type); } if (appCacheDir == null){ Log.e("getCacheDirectory","getCacheDirectory fail ,the reason is mobile phone unknown exception !"); }else { if (!appCacheDir.exists()&&!appCacheDir.mkdirs()){ Log.e("getCacheDirectory","getCacheDirectory fail ,the reason is make directory fail !"); } } return appCacheDir; } /** * 獲取SD卡快取目錄 * @param context 上下文 * @param type 資料夾型別 如果為空則返回 /storage/emulated/0/Android/data/app_package_name/cache * 否則返回對應型別的資料夾如Environment.DIRECTORY_PICTURES 對應的資料夾為 .../data/app_package_name/files/Pictures * {@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}.or 自定義資料夾名稱 * @return 快取目錄資料夾 或 null(無SD卡或SD卡掛載失敗) */ public static File getExternalCacheDirectory(Context context,String type) { File appCacheDir = null; if( Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { if (TextUtils.isEmpty(type)){ appCacheDir = context.getExternalCacheDir(); }else { appCacheDir = context.getExternalFilesDir(type); } if (appCacheDir == null){// 有些手機需要通過自定義目錄 appCacheDir = new File(Environment.getExternalStorageDirectory(),"Android/data/"+context.getPackageName()+"/cache/"+type); } if (appCacheDir == null){ Log.e("getExternalDirectory","getExternalDirectory fail ,the reason is sdCard unknown exception !"); }else { if (!appCacheDir.exists()&&!appCacheDir.mkdirs()){ Log.e("getExternalDirectory","getExternalDirectory fail ,the reason is make directory fail !"); } } }else { Log.e("getExternalDirectory","getExternalDirectory fail ,the reason is sdCard nonexistence or sdCard mount fail !"); } return appCacheDir; } /** * 獲取記憶體快取目錄 * @param type 子目錄,可以為空,為空直接返回一級目錄 * @return 快取目錄資料夾 或 null(建立目錄檔案失敗) * 注:該方法獲取的目錄是能供當前應用自己使用,外部應用沒有讀寫許可權,如 系統相機應用 */ public static File getInternalCacheDirectory(Context context,String type) { File appCacheDir = null; if (TextUtils.isEmpty(type)){ appCacheDir = context.getCacheDir();// /data/data/app_package_name/cache }else { appCacheDir = new File(context.getFilesDir(),type);// /data/data/app_package_name/files/type } if (!appCacheDir.exists()&&!appCacheDir.mkdirs()){ Log.e("getInternalDirectory","getInternalDirectory fail ,the reason is make directory fail !"); } return appCacheDir; }