Android development: Calculate the cache size and clear the cache

Source: Internet
Author: User

Android development: Calculate the cache size and clear the cache

The project encountered the function of calculating the cache size and clearing the cache. This is a common function, which is available in almost every APP. I thought the implementation was simple and I searched a lot on the Internet, I found that none of them meet my needs, and often deleted results were ineffective. So I found some other materials and finally completed them after a long time.

The following functions of this class are very simple: calculate the total size of your cache, regardless of internal cache or external cache, and clear the cache, including internal and external cache, please test it yourself, effect lever.

 

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 (co Ntext. 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 ++) {boolean success = deleteDir (new File (dir, children [I]); if (! Success) {return false ;}} return dir. delete ();} // get the file // Context. getExternalFilesDir () --> SDCard/Android/data/the package name/files/directory of your application. Generally, save some long-time data // Context. getExternalCacheDir () --> SDCard/Android/data/your application package name/cache/directory, which generally stores 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 is a file below if (fileList [I]. isDirectory () {size = size + getFolderSize (fileList [I]);} else {size = size + fileList [I]. length () ;}} catch (Exception e) {e. printStackTrace ();} return size;}/*** formatting unit ** @ param size * @ return */public static String getFormatSize (double size) {double kiloByte = size/1024; if (kiloByte <1) {// return size + "Byte"; return "0 K";} double megaByte = kiloByte/1024; if (megaByte <1) {BigDecimal result1 = new BigDecimal (Double. toString (kiloByte); return result1.setScale (2, BigDecimal. ROUND_HALF_UP ). toPlainString () + "KB";} double gigaByte = megaByte/1024; if (gigaByte <1) {BigDecimal result2 = new BigDecimal (Double. toString (megaByte); return result2.setScale (2, 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 the project, you can use the getTotalCacheSize (Context) method to clear the cache and use the clearAllCache (Context) method.

I checked a lot of Methods written by others on the Internet before, and the query can be used. This is often invalid when clearing. The invalid method is as follows:

 

private static void deleteFilesByDirectory(File directory) {          if (directory != null && directory.exists() && directory.isDirectory()) {              for (File item : directory.listFiles()) {                  item.delete();              }          }      }  

It should be a directory issue. The related links are as follows:

 

 

 

 


 

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.