Android empty application internal file cache _android

Source: Internet
Author: User

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.

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.