Android get SD card folder size and delete folder operation

Source: Internet
Author: User

floatCacheSize = Dirsize (NewFile (environment.getexternalstoragedirectory () + appconstants.app_cache_folder)/1024.0f/1024.0f; Tvcachesize.settext ( (int) (CacheSize *))/100.0f + "M");/*** Return the size of a directory in bytes*/Private Longdirsize (File dir) {if(Dir.exists ()) {Longresult = 0; File[] FileList=dir.listfiles (); for(inti = 0; i < filelist.length; i++) {//Recursive Call if it ' s a directoryif(Filelist[i].isdirectory ()) {result+=dirsize (Filelist[i]);} Else {//Sum The file size in bytesResult + =filelist[i].length ();}}returnResult//return the file size}return0;} Caser.id.clearcachelayout:Try{deleterecursive (NewFile (environment.getexternalstoragedirectory () +appconstants.app_cache_folder)); Toast.maketext (mactivity,"Cache cleared", Toast.length_short). Show ();floatCacheSize = Dirsize (NewFile (environment.getexternalstoragedirectory () + appconstants.app_cache_folder)/1024.0f/1024.0f; Tvcachesize.settext ( (int) (CacheSize *))/+ "M");} Catch(Exception e) {e.printstacktrace ();} Break;/*** Delete all folders and files under a folder * *@paramDelpath*/Private voiddeleterecursive (File fileordirectory) {if(Fileordirectory.isdirectory ()) for(File child:fileOrDirectory.listFiles ()) deleterecursive (child); Fileordirectory.delete ();}

Read Assets file contents

//get the file from the Assets folder and read the data Publicstring Getfromassets (String fileName) {string result= ""; Try{InputStream in=getresources (). Getassets (). open (fileName);//Gets the number of bytes in the fileintLenght =in.available ();//Create a byte arraybyte[] buffer =New byte[lenght];//read the data in a file into a byte arrayin.read (buffer); result=encodingutils.getstring (buffer, ENCODING);} Catch(Exception e) {e.printstacktrace ();}returnresult;}}

Android get SD card folder size and delete folder operation

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.