Welcome

首页 / 移动开发 / Android / Android编程获取sdcard卡信息的方法

本文实例讲述了Android编程获取sdcard卡信息的方法。分享给大家供大家参考,具体如下:
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的SIZElong nBlocSize = statfs.getBlockSize();// 获取可供程序使用的Block的数量long nAvailaBlock = statfs.getAvailableBlocks();// 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)long nFreeBlock = statfs.getFreeBlocks();SDCardInfo info = new SDCardInfo();// 计算SDCard 总容量大小MBinfo.total = nTotalBlocks * nBlocSize;// 计算 SDCard 剩余大小MBinfo.free = nAvailaBlock * nBlocSize;return info; } catch (IllegalArgumentException e) {Log.e(LOG_TAG, e.toString()); }}return null;}SDCardInfo sdCardInfo = Util.getSDCardInfo();// sd卡总容量sdCardInfo.total// sd卡剩余容量sdCardInfo.free
希望本文所述对大家Android程序设计有所帮助。