[Android] getCacheDir (), getFilesDir (), getExternalFilesDir (), getExternalCacheDir (), getfilesdir

Source: Internet
Author: User

[Android] getCacheDir (), getFilesDir (), getExternalFilesDir (), getExternalCacheDir (), getfilesdir

The getCacheDir () method is used to obtain the/data/<application package>/cache directory.

The getFilesDir () method is used to obtain the/data/<application package>/files directory.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If an application needs to save data to its mobile phone during running, it generally stores the data in SDcard.
Most applications directly create a folder under the root directory of the SDCard and save the data in the folder.
In this way, after the application is uninstalled, the data is retained in the SDCard, leaving junk data.
What if you want to clear the data related to the application after your application is uninstalled?

The Context. getExternalFilesDir () method can be used to obtain the SDCard/Android/data/package name/files/directory of your application. Generally, some data is stored for a long time.
The Context. getExternalCacheDir () method can be used to obtain SDCard/Android/data/your application package name/cache/directory, which generally stores temporary cache data.

If the above method is used, after your application is uninstalled by the user, all the files in the SDCard/Android/data/package name/directory of your application will be deleted, no spam information is left.

In addition, the preceding two directories correspond to the "Clear Data" and "Clear cache" options in Settings> Application> application details.


Do not store the downloaded content in the preceding directory.


Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------

A good program will write a special method to obtain the cache address, as shown below:

[Java]View plaincopy
  1. Public String getDiskCacheDir (Context context ){
  2. String cachePath = null;
  3. If (Environment. MEDIA_MOUNTED.equals (Environment. getExternalStorageState ())
  4. |! Environment. isExternalStorageRemovable ()){
  5. CachePath = context. getExternalCacheDir (). getPath ();
  6. } Else {
  7. CachePath = context. getCacheDir (). getPath ();
  8. }
  9. Return cachePath;
  10. }
You can see that when the SD card exists or the SD card cannot be removed, call the getExternalCacheDir () method to obtain the cache path. Otherwise, call the getCacheDir () method to obtain the cache path. The obtained path is/sdcard/Android/data/<application package>/cache, the latter obtains the path/data/<application package>/cache.



Reference: http://blog.csdn.net/beihai1212/article/details/7055899

Http://blog.csdn.net/zhiyi2010/article/details/13017171

Http://blog.csdn.net/guolin_blog/article/details/28863651



When the android program scans for storage, if the API: EnvironmentgetExternalStorageDirectory () getPath () is used

You can first determine Environment. getExternalStorageDirectory (). getParentFile (). If null is returned, there is no parent path, and Environment. getExternalStorageDirectory (). getPath () is the current parent path.

Android development: folder in which filePath is stored

Environment. getDataDirectory () =/data
Environment. getDownloadCacheDirectory () =/cache
Environment. getExternalStorageDirectory () =/mnt/sdcard
Environment. getExternalStoragePublicDirectory ("test") =/mnt/sdcard/test
Environment. getRootDirectory () =/system
GetPackageCodePath () =/data/app/com.my.app-1.apk
GetPackageResourcePath () =/data/app/com.my.app-1.apk
GetCacheDir () =/data/com. my. app/cache
GetDatabasePath ("test") =/data/com. my. app/databases/test
GetDir ("test", Context. MODE_PRIVATE) =/data/com. my. app/app_test
GetExternalCacheDir () =/mnt/sdcard/Android/data/com. my. app/cache
GetExternalFilesDir ("test") =/mnt/sdcard/Android/data/com. my. app/files/test
GetExternalFilesDir (null) =/mnt/sdcard/Android/data/com. my. app/files
GetFilesDir () =/data/com. my. app/files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.