Get an instance of the Android application specific cache storage directory

  • 2021-10-11 19:35:37
  • OfStack

If you want to get rid of the embarrassment of using cached directories: Can't find directories? Forgot to apply for read and write permission? Afraid of polluting users' storage space? ..... Please look down

SD Card Cache Directory

When an application needs to cache pictures or files in an SD card, it needs to apply for creating a directory. There are the following ways

We can call the application-specific directory through 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(); 

The above two directories are exclusive to the current app. When the application is deleted, the files in the above directories will also be emptied

Memory cache directory

There are two memory cache addresses relative to the application-specific SD card cache:


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

The files in these two directories will also be emptied with the deletion of app

When the system version is greater than or equal to 4.4, it is not necessary to apply for the read-write permission of SD card to read and write files to the directories obtained by the above four API calls, so it is not necessary to apply for the read-write permission dynamically when using the 6.0 and above systems

Precautions for use

When storing relatively large files, such as pictures, files are stored in the directory corresponding to SD card

The memory cache directory of the application can only be read and written by the application itself, but not by external applications, such as camera applications (memory directory read and write permissions: rwxr-xx, SD card cache directory read and write permissions: rwxrwx-)

Even if the above directory is obtained by user-defined path, it is not necessary to apply for SD card read-write permission when the system version is greater than or equal to 4.4

Application and method encapsulation of API


/**
 *  Get the application-specific cache directory 
 * android 4.4 And above systems do not need to apply SD Card read-write permission 
 *  Therefore, there is no need to consider 6.0 System dynamic application SD Card read-write permission problem, which is automatically emptied after the application is uninstalled   The storage space of the user will not be polluted 
 * @param context  Context 
 * @param type  Folder type   Can be empty, and if it is empty, it returns API Get 1 Level directory 
 * @return  Cache folder   If not SD Card or SD If there is a problem with the card, return to the memory cache directory, otherwise it will be returned first SD Card cache directory 
 */
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;
}

/**
 *  Get SD Card cache directory 
 * @param context  Context 
 * @param type  Folder type   Returns if it is empty  /storage/emulated/0/Android/data/app_package_name/cache
 *        Otherwise, the folder of the corresponding type is returned, such as Environment.DIRECTORY_PICTURES  The corresponding folder is  .../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  Custom folder name 
 * @return  Cache directory folder   Or  null (None SD Card or SD Card mount failed) 
 */
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){//  Some phones need to use custom directories 
      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;
}

/**
 *  Get the memory cache directory 
 * @param type  Subdirectory, which can be empty, and return directly if it is empty 1 Level directory 
 * @return  Cache directory folder   Or  null (Failed to create directory file) 
 *  Note: The directory obtained by this method can be used by the current application itself, and the external application has no read-write permission, such as   System camera application 
 */
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;
}

Related articles: