Skip to content

Instantly share code, notes, and snippets.

@piyipaya
Created March 18, 2015 13:22
Show Gist options
  • Select an option

  • Save piyipaya/904e8d101197a78c60e2 to your computer and use it in GitHub Desktop.

Select an option

Save piyipaya/904e8d101197a78c60e2 to your computer and use it in GitHub Desktop.
android get sdcard free size (from miui.fileexplorer)
//https://github.com/MiCode/FileExplorer/blob/master/src/net/micode/fileexplorer/Util.java
public static SDCardInfo getSDCardInfo() {
String sDcString = android.os.Environment.getExternalStorageState();
if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
File pathFile = android.os.Environment.getExternalStorageDirectory();
try {
android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
// 获取SDCard上BLOCK总数
long nTotalBlocks = statfs.getBlockCount();
// 获取SDCard上每个block的SIZE
long nBlocSize = statfs.getBlockSize();
// 获取可供程序使用的Block的数量
long nAvailaBlock = statfs.getAvailableBlocks();
// 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)
long nFreeBlock = statfs.getFreeBlocks();
SDCardInfo info = new SDCardInfo();
// 计算SDCard 总容量大小MB
info.total = nTotalBlocks * nBlocSize;
// 计算 SDCard 剩余大小MB
info.free = nAvailaBlock * nBlocSize;
return info;
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, e.toString());
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment