Android获取RAM大小,内部存储大小,外部存储大小
2015-11-25 14:12
357 查看
public class FileSizeUtil { private static final int ERROR = -1; /** * SDCARD是否存 */ public static boolean externalMemoryAvailable() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); } /** * 获取手机内部剩余存储空间 * * @return */ public static long getAvailableInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } /** * 获取手机内部总的存储空间 * * @return */ public static long getTotalInternalMemorySize() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } /** * 获取SDCARD剩余存储空间 * * @return */ public static long getAvailableExternalMemorySize() { if (externalMemoryAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return availableBlocks * blockSize; } else { return ERROR; } } /** * 获取SDCARD总的存储空间 * * @return */ public static long getTotalExternalMemorySize() { if (externalMemoryAvailable()) { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return totalBlocks * blockSize; } else { return ERROR; } } /** * 获取系统总内存 * * @param context 可传入应用程序上下文。 * @return 总内存大单位为B。 */ public static long getTotalMemorySize(Context context) { String dir = "/proc/meminfo"; try { FileReader fr = new FileReader(dir); BufferedReader br = new BufferedReader(fr, 2048); String memoryLine = br.readLine(); String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:")); br.close(); return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l; } catch (IOException e) { e.printStackTrace(); } return 0; } /** * 获取当前可用内存,返回数据以字节为单位。 * * @param context 可传入应用程序上下文。 * @return 当前可用内存单位为B。 */ public static long getAvailableMemory(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); am.getMemoryInfo(memoryInfo); return memoryInfo.availMem; } private static DecimalFormat fileIntegerFormat = new DecimalFormat("#0"); private static DecimalFormat fileDecimalFormat = new DecimalFormat("#0.#"); /** * 单位换算 * * @param size 单位为B * @param isInteger 是否返回取整的单位 * @return 转换后的单位 */ public static String formatFileSize(long size, boolean isInteger) { DecimalFormat df = isInteger ? fileIntegerFormat : fileDecimalFormat; String fileSizeString = "0M"; if (size < 1024 && size > 0) { fileSizeString = df.format((double) size) + "B"; } else if (size < 1024 * 1024) { fileSizeString = df.format((double) size / 1024) + "K"; } else if (size < 1024 * 1024 * 1024) { fileSizeString = df.format((double) size / (1024 * 1024)) + "M"; } else { fileSizeString = df.format((double) size / (1024 * 1024 * 1024)) + "G"; } return fileSizeString; } }
相关文章推荐
- Android之toolbar的使用
- IntentService简介
- Android 加载不同 DPI 资源与内存消耗间的关系
- Android代码生成TextView,Button,CheckBox等控件的设置技巧
- 两分钟彻底让你明白Android Activity生命周期(图文)!
- android数据解析之json解析和json字符串创建
- Android TextView文字旋转45°
- java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap...
- Android ListView滑动过程中图片显示重复错位闪烁问题解决
- Android性能分析工具——TraceView
- Android从文件目录中写入和读取图片
- android 项目工程之框架搭建
- Android 万能适配器
- android textview 设置不同的颜色和大小
- android编译分析之8—envsetup.mk
- Android开发ScrollView上下左右滑动事件冲突整理一(根据事件)
- RadioGroup调用check(id)方法时,OnCheckedChangedListener被多次回调问题
- Android自定义ProgressDialog
- 如何将Android数据库操作通用化(二)
- Android 利用ScrollView实现布局自动滚动