How do you get rid of images loaded in Android apps and some data caches? Recently in the project encountered this problem, search the Internet to find a lot of source code, the synthesis of these sources, the following to tell us how to effectively clear the application memory cache information?
The purge application internal cache consists of the following major modules:
One, clear apply all local Database cache
/** * Clears all databases for this application (/data/data/com.xxx.xxx/databases) * * @param context */public
static void Cleandatabases (Context Co ntext) {
deletefilesbydirectory (new File ("/data/data/"
+ context.getpackagename () + "/databases"));
Second, the elimination of the application Sharepreference data
/**
* * Clear this application sharedpreference (/data/data/com.xxx.xxx/shared_prefs) * * @param * Context/public
static void Cleansharedpreference {
deletefilesbydirectory (new File ("/data/data/"
+ Context.getpackagename () + "/shared_prefs");
}
Iii. Clear Data/data/com.xxx.xxx/files Data
/** * Clear/data/data/com.xxx.xxx/files content under * * @param context *
/public static void Cleanfiles {
Fi Le sddir = environment.getexternalstoragedirectory ()//Fetch with directory
Sddir = new File (Sddir.getpath () +file.separator+ " xxx ");
Sddir = new File (Sddir.getpath () +file.separator+ "files");
Deletefilesbydirectory (Sddir);
}
Iv. clear the data under the external cache
/** * * Clear the contents of the external cache (/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param * Context/public static void clean
Externalcache (Context context) {if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
Deletefilesbydirectory (Context.getexternalcachedir ()); How do you finally get the size of the data? Paste source code:/** * Get folder Size * @param file File instance * @return Long/public static long Getfoldersize (Java.io.Fil
E file) {long size = 0;
try {java.io.file[] filelist = File.listfiles ();
for (int i = 0; i < filelist.length i++) {if (Filelist[i].isdirectory ()) {
Size = size + getfoldersize (filelist[i]);
}else{size = size + filelist[i].length ();
catch (Exception e) {//TODO auto-generated catch block E.printstacktrace ();
}//return size/1048576;
return size;
}
The main thing is to traverse the folder, get the size of the file in the folder, get the cache size. If some cached pictures in the application do not want to be deleted, or put the cached picture in the specified folder, do not delete the folder when you delete it, so that you do not reload the picture because the cache is empty.
The above is about the Android empty application of internal file caching method detailed introduction, I hope to help you learn.