Android Storage Path Selection Method

  • 2021-10-11 19:30:42
  • OfStack

There are two places where Android can be stored, one is the built-in storage space of mobile phone, and the other is the external SD card. The built-in storage space is generally small, so it is recommended to store the application cache in the external SD card.

How to get the storage path in Android system?


 public static void getRootPath(Context context) {
  Log.d("ExternalDirectory->", Environment.getExternalStorageDirectory().getPath());

  Log.d("ExternalCacheDir->", context.getExternalCacheDir().getPath());

  Log.d("CacheDir->", context.getCacheDir().getPath());

  Log.d("FilesDir->", context.getFilesDir().getPath());
 }

The corresponding output result above is


ExternalDirectory->: /storage/emulated/0 
ExternalCacheDir->: /storage/emulated/0/Android/data/zhuwentao.com.pathdemo/cache 
CacheDir->: /data/data/zhuwentao.com.pathdemo/cache 
FilesDir->: /data/data/zhuwentao.com.pathdemo/files

The application cache is recommended to be stored in the path obtained through getExternalCacheDir (). getPath (), where zhuwentao. com. pathdemo will vary according to your application package name. There are several main advantages of this path.

1: This path is in the SD card, so don't worry about the lack of built-in storage space of the mobile phone, as long as the SD card is big enough.

2: This path is the default application cache path of Android system, which corresponds to the "Clear Cache" and "Clear Data" buttons of application details in mobile phone application settings.

3: When we uninstall the application, the data under this path will be emptied, thus avoiding the problem of residual data after some applications are uninstalled.

If you need to save the data downloaded by users, it is not recommended to store it in getExternalCacheDir (). You can create a new folder under Environment. getExternalStorageDirectory () to store the data downloaded by users.

When selecting the storage path, we should also consider whether the user's mobile phone has an SD card, and then make a choice to judge whether the user has an SD card through the following code:


 /**
  *  Get the storable path of the mobile phone 
  * @param context  Context 
  * @return  Memorable path of mobile phone 
  */
 public static String getRootPath(Context context) {
  //  Whether there is SD Card 
  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
    || !Environment.isExternalStorageRemovable()) {
   return context.getExternalCacheDir().getPath(); //  Have 
  } else {
   return context.getCacheDir().getPath(); //  None 
  }
 }

If there is an SD card or the SD card cannot be removed, the path under the SD card is obtained, and if there is no SD card, the application cache path is obtained.

Sometimes getExternalStorageDirectory () can't get an external SD card. Why?

Before Android4.0, Environment. getExternalStorageDirectory () obtained the external SD card path, but after 4.0, because many mobile phones have large self-contained memory and do not need external SD card, Android divided the built-in memory into one part and used it as SD card, resulting in different Android card paths obtained by different versions of Android systems.

It should be noted that the paths of SD cards of different mobile phone manufacturers are also different. Some mobile phones get the paths of "/mnt/sdcard/…", while others get the paths of "/storage/sdcard0/…", so the paths of SD cards must not be written dead.


Related articles: