Android get folder, file size in B, KB, MB, GB
Filesizeutil
Public classFilesizeutil { Public StaticFinalintSizetype_b =1;//gets the double value of the file size unit B Public StaticFinalintsizetype_kb =2;//gets the double value of the file size in kilobytes Public StaticFinalintSIZETYPE_MB =3;//gets a double value in megabytes of file size Public StaticFinalintSIZETYPE_GB =4;//gets the double value of the file size unit in GB/** * Gets the size of the specified unit of file specified by the file * @param filePath file path * @param sizeType gets the size of type 1 is B, 2 is KB, 3 is MB, 4 is gb* @return the size of the double value*/ Public Static DoubleGetfileorfilessize (String FilePath,intSizeType) {File File=NewFile (filePath);LongBlocksize=0;Try {if(File.isdirectory ()) {blockSize=getfilesizes (file);}Else{blockSize=getfilesize (file);}} Catch(Exception e) {e.printstacktrace (); LOG.E ("Get File Size","Get failed!");}returnformetfilesize (blockSize, SizeType);}/** * Call this method to automatically calculate the size of the specified file or specified folder * @param filePath file path * @return calculated string with B, KB, MB, GB*/ Public Staticstring Getautofileorfilessize (String filePath) {File file=NewFile (filePath);LongBlocksize=0;Try {if(File.isdirectory ()) {blockSize=getfilesizes (file);}Else{blockSize=getfilesize (file);}} Catch(Exception e) {e.printstacktrace (); LOG.E ("Get File Size","Get failed!");}returnformetfilesize (blockSize);}/** * Get the specified file size * @param f* @return * @throws Exception*/Private Static Longgetfilesize (File file) throws exception{LongSize =0; if(File.exists ()) {FileInputStream fis=NULL; FIS=Newfileinputstream (file); Size=fis.available ();} Else{file.createnewfile (); LOG.E ("Get File Size","file does not exist!"); } returnsize;} /** * Get the specified folder * @param f* @return * @throws Exception*/Private Static Longgetfilesizes (File f) throws exception{LongSize =0; File flist[]=f.listfiles (); for(inti =0; i < flist.length; i++){if(Flist[i].isdirectory ()) {size= size +getfilesizes (Flist[i]);}Else{size=size +GetFileSize (Flist[i]);}}returnsize;}/** * Convert file Size * @param files * @return*/Private StaticString Formetfilesize (LongFileS) {DecimalFormat DF=NewDecimalFormat ("#.00"); String filesizestring=""; String wrongsize="0B";if(files==0){returnwrongsize;}if(FileS <1024x768) {filesizestring= Df.format ((Double) FileS) +"B"; }Else if(FileS <1048576) {filesizestring= Df.format ((Double) FileS/1024x768) +"KB";}Else if(FileS <1073741824) {filesizestring= Df.format ((Double) FileS/1048576) +"MB"; }Else{filesizestring= Df.format ((Double) FileS/1073741824) +"GB"; }returnfilesizestring;}/** Convert file size, specify type of conversion * @param files * @param sizeType * @return*/Private Static DoubleFormetfilesize (LongFileS,intSizeType) {DecimalFormat DF=NewDecimalFormat ("#.00");DoubleFilesizelong =0;Switch(sizeType) { CaseSizetype_b:filesizelong=double.valueof (Df.format (DoubleFileS)); Break; CaseSizetype_kb:filesizelong=double.valueof (Df.format (Double) FileS/1024x768)); Break; CaseSizetype_mb:filesizelong=double.valueof (Df.format (Double) FileS/1048576)); Break; CaseSizetype_gb:filesizelong=double.valueof (Df.format (Double) FileS/1073741824)); Break;default: Break;}returnFilesizelong;}
Android Get File size