Android 系统获取内存,ROM,SD卡大小
2015-08-13 15:36
561 查看
直接记录方法下来,我个人理解 RAM:指的是内存 ROM:内部存储 SD卡属于外部存储
我的小米2A运行得到的结果是:113MB/755MB (可用内存与系统差不多/系统标的是1GB)
875MB/3.49GB
3.17GB/9.37GB(可用sd卡与系统显示差不多/sd总大小系统显示10GB)
kukai电视结果:349MB/811MB
4.07GB/5.66GB (系统显示4.07GB/8GB)
没有sd卡但还是显示 4.07GB/5.66GB
-------------------------------附上课笔记-------------------------------------------------------------
MemoryInfooutInfo =
newActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
floatavailMem=outInfo.availMem/1024f/1024f;
//格式化这个文本字符串
DecimalFormatformat =
newDecimalFormat("###.00");
StringavailMemStr = format.format(availMem);
tv_avail_memory.setText(availMemStr+"MB");
单个进程占用内存:用DecimalFormat格式化得到的int值(参照上面)
MemoryInfo[]memoryInfo =
activityManager.getProcessMemoryInfo(pids);
intkbMemory
= memoryInfo[0].getTotalPrivateDirty()
总内内存
Api中没有相关方法:方法一(很多软件这样做):可用上面两个相加
方法二(实际内存大小):Build类,用反射获得,从类中可取得内存大小
我的小米2A运行得到的结果是:113MB/755MB (可用内存与系统差不多/系统标的是1GB)
875MB/3.49GB
3.17GB/9.37GB(可用sd卡与系统显示差不多/sd总大小系统显示10GB)
kukai电视结果:349MB/811MB
4.07GB/5.66GB (系统显示4.07GB/8GB)
没有sd卡但还是显示 4.07GB/5.66GB
// 获取android当前可用内存大小 private String getAvailMemory() { ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo mi = new MemoryInfo(); am.getMemoryInfo(mi); //mi.availMem; 当前系统的可用内存 return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化 } //系统总内存大小 private String getTotalMemory() { String str1 = "/proc/meminfo";// 系统内存信息文件 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader( localFileReader, 8192); str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小 arrayOfString = str2.split("\\s+"); for (String num : arrayOfString) { Log.i(str2, num + "\t"); } initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte localBufferedReader.close(); } catch (IOException e) { } return Formatter.formatFileSize(getBaseContext(), initial_memory);// Byte转换为KB或者MB,内存大小规格化 } //ROM大小 private String getRomSpace() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); long availableBlocks = stat.getAvailableBlocks(); //long usedBlocks=totalBlocks-availableBlocks; String totalSize = Formatter.formatFileSize(getApplicationContext(), totalBlocks*blockSize); String availableSize = Formatter.formatFileSize(getApplicationContext(), availableBlocks*blockSize); return availableSize+"/"+totalSize; } //得到SD卡大小 private String getSdcardSize(){ File path = Environment.getExternalStorageDirectory();//得到SD卡的路径 StatFs stat = new StatFs(path.getPath());//创建StatFs对象,用来获取文件系统的状态 long blockCount = stat.getBlockCount(); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); String totalSize = Formatter.formatFileSize(getApplicationContext(), blockCount*blockSize);//格式化获得SD卡总容量 String availableSize = Formatter.formatFileSize(getApplicationContext(), availableBlocks*blockSize);//获得SD卡可用容 return availableSize+"/"+totalSize; }
-------------------------------附上课笔记-------------------------------------------------------------
20.5.ActivityManager获取内存大小
剩余可用内存MemoryInfooutInfo =
newActivityManager.MemoryInfo();
activityManager.getMemoryInfo(outInfo);
floatavailMem=outInfo.availMem/1024f/1024f;
//格式化这个文本字符串
DecimalFormatformat =
newDecimalFormat("###.00");
StringavailMemStr = format.format(availMem);
tv_avail_memory.setText(availMemStr+"MB");
单个进程占用内存:用DecimalFormat格式化得到的int值(参照上面)
MemoryInfo[]memoryInfo =
activityManager.getProcessMemoryInfo(pids);
intkbMemory
= memoryInfo[0].getTotalPrivateDirty()
总内内存
Api中没有相关方法:方法一(很多软件这样做):可用上面两个相加
方法二(实际内存大小):Build类,用反射获得,从类中可取得内存大小
相关文章推荐
- 创建Material Design风格Android应用--自定义阴影和裁剪视图
- Android笔记(十一) Android中的布局——网格布局
- NJUPT JAVA语言 android拼图小游戏设计
- Android开发必知--几种不同对话框的实现
- Android开发技巧——使用PopupWindow实现弹出菜单
- Android应用开发之将SQLite和APK一起打包的方法
- android 处理按键动作
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- android studio用的几个插件
- android 开发用到key时keystore处理
- 【转】Android 之 下拉框(Spinner)的使用
- android—sqlite
- android工程更改api版本
- Android源码学习之ListView的复用回收机制剖析.
- android的序列化接口Parcelable
- Android之Dialog详解
- android ant 打包error
- Android Bundle对象传值
- Android模仿iPhone View旋转刷新数据动画详解
- 【转】Android通过JNI调用驱动程序(完全解析实例)