Android Tool classes for viewing folder sizes and deleting folders

  • 2021-09-11 21:20:29
  • OfStack

When ANR or exception occurs in the program during development, we will store the information locally and upload it to the server, so that we can find the problem and modify the problem in real time.

Then we need to delete the file after obtaining it. The following is a tool class written to view the folder size and delete the folder.


import android.text.TextUtils;

import java.io.File;
import java.math.BigDecimal;


public class StorageCleanUtils {

 /**
  *  Get folder size ( Recursion )
  *
  * @param file File Instances 
  * @return long
  */
 public static long getFolderSize(java.io.File file) {

  long size = 0;
  try {
   java.io.File[] fileList = file.listFiles();
   for (int i = 0; i < fileList.length; i++) {
    if (fileList[i].isDirectory()) {
     size = size + getFolderSize(fileList[i]);

    } else {
     size = size + fileList[i].length();

    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return size;
 }


 /**
  *  Gets the current folder size, not recursive subfolders 
  *
  * @param file
  * @return
  */
 public static long getCurrentFolderSize(File file) {
  long size = 0;
  try {
   java.io.File[] fileList = file.listFiles();
   for (int i = 0; i < fileList.length; i++) {
    if (fileList[i].isDirectory()) {
     // Skip subfolders 

    } else {
     size = size + fileList[i].length();

    }
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return size;
 }


 /**
  *  Delete files and directories in specified directories 
  *
  * @param deleteThisPath
  * @param filePath
  * @return 
  */
 public static boolean deleteFolderFile(String filePath, boolean deleteThisPath) {
  if (!TextUtils.isEmpty(filePath)) {
   try {
    File file = new File(filePath);
    if (file.isDirectory()) {//  Processing directory 
     File files[] = file.listFiles();
     for (int i = 0; i < files.length; i++) {
      deleteFolderFile(files[i].getAbsolutePath(), true);
     }
    }
    if (deleteThisPath) {
     if (!file.isDirectory()) {//  If it is a file, delete 
      file.delete();
     } else {//  Directory 
      if (file.listFiles().length == 0) {//  There are no files or directories under the directory, delete them 
       file.delete();
      }
     }
    }
    return true;
   } catch (Exception e) {
    e.printStackTrace();
    return false;
   }
  }
  return false;
 }


 /**
  *  Delete files in specified directory 
  *
  * @param filePath
  * @return
  */
 public static void deleteFile(String filePath) {
  if (!TextUtils.isEmpty(filePath)) {
   try {
    File file = new File(filePath);
    java.io.File[] fileList = file.listFiles();
    for (int i = 0; i < fileList.length; i++) {
     if (!fileList[i].isDirectory()) {
      fileList[i].delete();
     }
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }


 /**
  *  Formatting unit 
  *
  * @param size
  * @return
  */
 public static String getFormatSize(double size) {
  double kiloByte = size / 1024;
  if (kiloByte < 1) {
   return size + "Byte(s)";
  }

  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";
 }

}

Related articles: