android计算每个目录剩余空间丶总空间以及SD卡剩余空间

ublic class MemorySpaceCheck {
/**
* 计算剩余空间
* @param path
* @return
*/
public static String getAvailableSize(String path)
{
StatFs fileStats = new StatFs(path);
fileStats.restat(path);
return String.valueOf(fileStats.getAvailableBlocks() * fileStats.getBlockSize()); // 注意与fileStats.getFreeBlocks()的区别
// return getPrintSize((long) fileStats.getFreeBlocks() * fileStats.getFreeBlocks()); // 注意与fileStats.getFreeBlocks()的区别
}
/**
* 跟上面的方法含义一样,返回值和方法名不同
* @param path
* @return
*/
public static double getAvailableSizes(String path)
{
StatFs fileStats = new StatFs(path);
fileStats.restat(path);
return Double.valueOf(fileStats.getAvailableBlocks() * fileStats.getBlockSize()); // 注意与fileStats.getFreeBlocks()的区别
// return getPrintSize((long) fileStats.getFreeBlocks() * fileStats.getFreeBlocks()); // 注意与fileStats.getFreeBlocks()的区别
}
/**
* 计算总空间
* @param path
* @return
*/
public static String getTotalSize(String path)
{
StatFs fileStats = new StatFs(path);
fileStats.restat(path);
return String.valueOf(fileStats.getBlockCount() * fileStats.getBlockSize());
}

/**
* 计算SD卡的剩余空间
* @return 剩余空间
*/
public static String getSDAvailableSize()
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
return getAvailableSize(Environment.getExternalStorageDirectory().toString());
}

return null;
}

/**
* 计算系统的剩余空间
* @return 剩余空间
*/
public static String getSystemAvailableSize()
{
// context.getFilesDir().getAbsolutePath();
return getAvailableSize("/data");
}
/**
* 是否有足够的空间
* @param filePath 文件路径,不是目录的路径
* @return
*/
public static boolean hasEnoughMemory(String filePath)
{
File file = new File(filePath);
long length = file.length();
if (filePath.startsWith("/sdcard") || filePath.startsWith("/mnt/sdcard"))
{
return Integer.parseInt(getSDAvailableSize()) > length;
}
else
{
return Integer.parseInt(getSystemAvailableSize()) > length;
}

}

/**
* 获取SD卡的总空间
* @return
*/
public static String getSDTotalSize()
{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
return getTotalSize(Environment.getExternalStorageDirectory().toString());
}

return null;
}

/**
* 获取系统可读写的总空间
* @return
*/
public static String getSysTotalSize()
{
return getTotalSize("/data");
}

/**
* 格式化单位
* 转换为B,GB等等
* @param size
* @return
*/
public static String getFormatSize(double size) {
double kiloByte = size / 1024;
if (kiloByte < 1) {
return size + "Byte";
}

double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
return result1.setScale(1, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "KB";
}

double gigaByte = megaByte / 1024;
if (gigaByte < 1) {
BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
return result2.setScale(1, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "MB";
}

double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {
BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
return result3.setScale(1, BigDecimal.ROUND_HALF_UP)
.toPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString()
+ "TB";
}

上一篇:Quartz学习总结(1)——Spring集成Quartz框架


下一篇:Spring+Struts集成(第二种方案)