Android can be used to store two places, one is the phone built-in storage space, an external SD card, the built-in storage space is generally relatively small, so the application of the cache is recommended to store the external SD card.
How do I get the path to the storage in the Android system?
Public Static voidGetrootpath (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 of the 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 cache recommendations for the app are stored in the path obtained through Getexternalcachedir (). GetPath (), where the Zhuwentao.com.pathdemo will differ depending on the name of your application package, and there are several benefits to this path.
One: The path is in the SD card, so do not worry about the phone built-in storage space, as long as the SD card is large enough.
Second: The path is the default app cache path for Android, which corresponds to the "clear cache" and "Erase Data" buttons for application details in the app settings.
Third: When we uninstall the application, the data under that path is also emptied, which avoids the problem of some applications having residual data after uninstallation.
If you need to save the user's downloaded data, it is not recommended to store in Getexternalcachedir (), you can create a new folder under Environment.getexternalstoragedirectory () to store the data downloaded by the user.
When we choose the storage path, we also have to consider whether the user's phone has an SD card, and then choose, the following code to determine whether the user has an SD card:
/** * Get phone can store path * @param context context * @return phone can store path*/ Public StaticString Getrootpath (context context) {//do you have an SD card ? if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)|| !environment.isexternalstorageremovable ()) { returnContext.getexternalcachedir (). GetPath ();//have a}Else { returnContext.getcachedir (). GetPath ();//No } }
If you have an SD card or an SD card that is not removable, get the path under the SD card and not the application cache path.
Sometimes getexternalstoragedirectory () how can not get External SD card, this is why?
Before Android4.0 Environment.getexternalstoragedirectory () Gets the external SD card path, After 4.0, since many of the phone's own memory is very large, do not need an external SD card, so Android put the built-in memory as a part of the SD card use, resulting in different versions of the Android system to obtain the SD card path will be the difference.
Note that each handset manufacturer's SD card path will also be different, some mobile phone access to the path is "/mnt/sdcard/...", and some get is "/storage/sdcard0/..." so absolutely cannot write dead SD card path.
Android Storage Path Selection