/** Get SD card info detail. */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public static SDCardInfo getSDCardInfo() { SDCardInfo sd = new SDCardInfo(); String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { sd.isExist = true; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { File sdcardDir = Environment.getExternalStorageDirectory(); StatFs sf = new StatFs(sdcardDir.getPath()); sd.totalBlocks = sf.getBlockCountLong(); sd.blockByteSize = sf.getBlockSizeLong(); sd.availableBlocks = sf.getAvailableBlocksLong(); sd.availableBytes = sf.getAvailableBytes(); sd.freeBlocks = sf.getFreeBlocksLong(); sd.freeBytes = sf.getFreeBytes(); sd.totalBytes = sf.getTotalBytes(); } } if (Log.isPrint) { Log.i(TAG, sd.toString()); } return sd; }
public static SDCardInfo getSDCardInfo() { String sDcString = Environment.getExternalStorageState(); if (sDcString.equals(Environment.MEDIA_MOUNTED)) { File pathFile = 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) { e.printStackTrace(); } } return null; }