1>实例代码:
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File path = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(path.getPath());
long blockSize = statFs.getBlockSize();
long totalBlocks = statFs.getBlockCount();
long availableBlocks = statFs.getAvailableBlocks();
String[] total = fileSize(totalBlocks * blockSize);
String[] available = fileSize(availableBlocks * blockSize);
int ss = (int) (((totalBlocks-availableBlocks) / (totalBlocks * 1.0)) * myProgressBar.getMax());
myProgressBar.setProgress(ss);
String text = available[0] + available[1] + "可用 \t总" + total[0] + total[1];
myTextView.setText(text);
} else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
String text = "SD CARD 已删除";
myTextView.setText(text);
}
2>获取sdcard状态
Environment.getExternalStorageState()
//获得扩展存储设备状态
MEDIA_BAD_REMOVAL//安全卸载前强制拔除
MEDIA_CHECKING//正在扫描
MEDIA_MOUNTED//已挂载
MEDIA_MOUNTED_READ_ONLY//只读
MEDIA_NOFS//sdcar是空的,或文件系统不支持
MEDIA_REMOVED//已移除
MEDIA_SHARED//未挂载,并以USB模式和计算机连接
MEDIA_UNMOUNTABLE//有sdcard,但无法挂载
MEDIA_UNMOUNTED//有sdcard,但未挂载
String | MEDIA_BAD_REMOVAL | getExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was removed before it was unmounted. |
String | MEDIA_CHECKING | getExternalStorageState() returns MEDIA_CHECKING if the media is present and being disk-checked |
String | MEDIA_MOUNTED | getExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access. |
String | MEDIA_MOUNTED_READ_ONLY | getExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present and mounted at its mount point with read only access. |
String | MEDIA_NOFS | getExternalStorageState() returns MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem |
String | MEDIA_REMOVED | getExternalStorageState() returns MEDIA_REMOVED if the media is not present. |
String | MEDIA_SHARED | getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage. |
String | MEDIA_UNMOUNTABLE | getExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present but cannot be mounted. |
String | MEDIA_UNMOUNTED | getExternalStorageState() returns MEDIA_UNMOUNTED if the media is present but not mounted. |
3>获取sdcard状态
File path = Environment.getExternalStorageDirectory();//获得sdcard路径
StatFs statFs = new StatFs(path.getPath());
long blockSize = statFs.getBlockSize();//获取块大小
long totalBlocks = statFs.getBlockCount();//获取总块数
long availableBlocks = statFs.getAvailableBlocks();//获取可用块数
long totalSize = totalBlocks*blockSize;//总大小
long availableSize = availableBlocks*blockSize;//可用大小