@SuppressLint("NewApi")
public static boolean getSDStatus() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 如果存储卡存在,则获取存储文件的路径
File path = Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());// 创建StatFs对象
long blockSize = sf.getBlockSizeLong();// 获得blockSize
long totalBlock = sf.getBlockCountLong();// 获得全部block
long availableBlock = sf.getAvailableBlocksLong();// 获取可用的block
// 用String数组来存放Block信息
String[] total = fileSize(totalBlock * blockSize);
String[] available = fileSize(availableBlock * blockSize);
Logs.d("test", Arrays.toString(total));
Logs.d("test", Arrays.toString(available));
String s = "SD卡中空间总共有:" + total[0] + total[1] + "\n";
s += "剩余空间大小:" + available[0] + available[1];
// tv.setText(s);
Log.i("test", "SD卡中空间总共有:" + total[0] + total[1]);
Log.i("test", "剩余空间大小:" + available[0] + available[1]);
return true;
} else if (Environment.getExternalStorageState().equals(
Environment.MEDIA_REMOVED)) {
// tv.setText("SD卡已移除");
Log.i("test", "SD卡已移除");
return false;
} else {
return false;
}
}
// 用来定义存储空间显示格式
public static String[] fileSize(long size) {
String s = "";
if (size > 1024) {
s = "KB";
size /= 1024;
if (size > 1024) {
s = "MB";
size /= 1024;
}
}
DecimalFormat df = new DecimalFormat();
df.setGroupingSize(3);
String[] result = new String[3];
result[0] = df.format(size);
result[1] = s;
return result;
}