Reprinted blog: http://www.2cto.com/kf/201503/385492.html
Project encountered in the calculation of the size of the cache and the function of emptying the cache, this very common function, almost every app has, thought the implementation is very simple, the web search a lot of, found are not in line with my needs, and often deleted no effect, so another find some information, toss for a long time, finally completed. The following features of this class are simple, calculate the total size of your cache, regardless of the internal cache or external cache, and empty the cache, including internal and external caches together empty, please self-test, the effect of the leverage.
Public classDatacleanmanager { Public StaticString gettotalcachesize (Context context)throwsException {LongCacheSize =getfoldersize (Context.getcachedir ()); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {cacheSize+=getfoldersize (Context.getexternalcachedir ()); } returngetformatsize (cacheSize); } Public Static voidClearallcache (Context context) {Deletedir (Context.getcachedir ()); if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Deletedir (Context.getexterna Lcachedir ()); } } Private Static BooleanDeletedir (File dir) {if(dir! =NULL&&dir.isdirectory ()) {string[] Children=dir.list (); for(inti = 0; i < children.length; i++) { BooleanSuccess = Deletedir (NewFile (dir, children[i])); if(!success) { return false; } } } returnDir.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 LongGetfoldersize (File file)throwsException {LongSize = 0; Try{file[] fileList=File.listfiles (); for(inti = 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 (); } returnsize; } /*** Format Unit *@paramsize */ Public StaticString Getformatsize (Doublesize) { Doublekilobyte = size/1024; if(Kilobyte < 1) { return size + "Byte"; } Doublemegabyte = kilobyte/1024; if(Megabyte < 1) {BigDecimal result1=NewBigDecimal (double.tostring (kilobyte)); returnResult1.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "KB"; } DoubleGigaByte = megabyte/1024; if(GigaByte < 1) {BigDecimal result2=NewBigDecimal (double.tostring (megabyte)); returnResult2.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "MB"; } Doubleterabytes = gigabyte/1024; if(Terabytes < 1) {BigDecimal RESULT3=NewBigDecimal (double.tostring (gigaByte)); returnResult3.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "GB"; } BigDecimal RESULT4=NewBigDecimal (terabytes); returnResult4.setscale (2, bigdecimal.round_half_up). toplainstring ()+ "TB"; } }
When you need to check the cache size in your project, use the Gettotalcachesize (context) method, empty the cache, and use the Clearallcache (context) method
Before the online search a lot of other people write methods, query can be used, when emptying is often invalid, this invalid method is as follows:
Private Static void deletefilesbydirectory (File directory) { ifnull && directory.exists () & & Directory.isdirectory ()) { for (File item:directory.listFiles ()) { Item.delete (); }}}
Android compute cache size and empty the cache