Android compute cache size and empty the cache

Source: Internet
Author: User

The project encountered a function of calculating the cache size and emptying the cache, a very common feature that almost every APP has, thought the implementation is very simple, search a large number of online, found are not in line with my needs, and often deleted no effect, so another to find some information, tossing a pretty long, and finally finished. The following features of this class are simple enough to calculate the total size of your cache, regardless of the internal cache or external cache, and empty the cache, including both internal and external caches:

Public class datacleanmanager {         public  static string gettotalcachesize (Context context)  throws Exception {             long cacheSize =  Getfoldersize (Context.getcachedir ());             if  (Environment.getexternalstoragestate (). Equals (environment.media_mounted))  {                  cacheSize +=  Getfoldersize (Context.getexternalcachedir ());             }             return  Getformatsize (cacheSize);        }       Public static void clearallcache(Context context)  {        deletedir (Context.getCacheDir ());         if  (Environment.getexternalstoragestate (). Equals ( environment.media_mounted))  {              deletedir (Context.getexternalcachedir ());        }      }      private static boolean deletedir (File  dir)  {        if  (dir != null & & dir.isdirectory ())  {             String[] children = dir.list ();             for  (int i = 0; i < children.length; i++)  {        &nBsp;        boolean success = deletedir (New File ( Dir, children[i]);                 if  (!success)  {                     return false;                 }             }        }         return dir.delete ();    }           //  get File      //context.getexternalfilesdir ()  --> sdcard/ Android/data/your app's package name/files/  directory, usually put some long-time saved data       // Context.getexternalcachedir ()  -->  sdcard/android/data/your app package name/cache/directory, typically storing temporary cache data      public static long  Getfoldersize (File file)  throws Exception {          long size = 0;         try {              file[] filelist =  file.listfiles ();             for  (int i = 0; i < filelist.length; i++)  {                  //  If there are files                   if  (Filelist[i] . Isdirectory ())  {                      size = size + getfoldersize (Filelist[i]);                  } else {                      size =  size + filelist[i].length ();                  }              }         } catch  (exception e)  {              e.printstacktrace ();          }         return size;      }           /**      *  formatted units      *  @param  size     */     public static string getformatsize (double size)  {          double kiloByte = size / 1024;          if  (kilobyte < 1)  {             return size +  "Byte";          }           double megabyte  = kiloByte / 1024;         if  (megabyte &NBSP;&LT;&NBSP;1)  {              Bigdecimal result1 = new bigdecimal (double.tostring (kilobyte));              return result1.setscale (2,&NBSP;BIGDECIMAL.ROUND_HALF_UP)                       . Toplainstring ()  +  "KB";         }            double gigaByte = megaByte / 1024;          if  (gigabyte < 1)  {              bigdecimal result2 = new  bigdecimal (Double.tostring (megabyte));              return result2.setscale (2,&NBSP;BIGDECIMAL.ROUND_HALF_UP)                       .toplainstring ()  +  "MB";           }           double  terabytes = gigabyte / 1024;         if   (terabytes < 1)  {              bigdecimal result3 = new bigdecimal (double.tostring (gigaByte));              return result3.setscale (2,  BIGDECIMAL.ROUND_HALF_UP)                       .toplainstring ()  +  "GB";          }         bigdecimal result4 =  new bigdecimal (terabytes);         return  Result4.setscale (2, bigdecimal.ROUND_HALF_UP). toplainstring ()                   +  "TB";      }     }

When you need to check the cache size in your project, use the Gettotalcachesize (context) method and use the Clearallcache (context) method to clear the cache . Of course, the development of the APP is also required to conduct a full range of testing:www.ineice.com.


Android compute cache size and empty the cache

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.