ublic class MemorySpaceCheck { /** * 計算剩餘空間 * @param path * @return */ public static String getAvailableSize(String path) { StatFs fileStats = new Sta ...
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";
}