本文实例讲述了Android开发实现的获取sdcard大小及内存大小工具类。分享给大家供大家参考,具体如下:
public class SDCardUtil { /** * SD卡 * @param context */ public static void getSDCardInfo(Context context){ try { File path = Environment.getExternalStorageDirectory(); StatFs s = new StatFs(path.getPath()); long availableBlocks = s.getAvailableBlocksLong(); long blockCount = s.getBlockCountLong(); long blockSize = s.getBlockSizeLong(); long totalsize = blockSize * blockCount; long availsize = blockSize * availableBlocks; String totalsizeStr = Formatter.formatFileSize(context, totalsize); String availsizeStr = Formatter.formatFileSize(context, availsize); } catch (Exception e) { e.printStackTrace(); } } /** * 手机内存 * @param context */ public static void getDataInfo(Context context){ try { File path = Environment.getDataDirectory(); StatFs s = new StatFs(path.getPath()); long availableBlocks = s.getAvailableBlocksLong(); long blockCount = s.getBlockCountLong(); long blockSize = s.getBlockSizeLong(); long totalsize = blockSize * blockCount; long availsize = blockSize * availableBlocks; String totalsizeStr = Formatter.formatFileSize(context, totalsize); String availsizeStr = Formatter.formatFileSize(context, availsize); } catch (Exception e) { e.printStackTrace(); } }}
希望本文所述对大家Android程序设计有所帮助。
新闻热点
疑难解答