众所周知,Android的手机上的信息大部分都是可以通过代码获取的,比如说爱奇艺的离线储存功能上有一个最大储存大小/剩余储存大小的功能。
获取SD卡上的储存信息:
/** * 获得SD卡总大小 * * @return */ private String getSDTotalSize() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); return Formatter.formatFileSize(this, blockSize * totalBlocks); } /** * 获得sd卡剩余容量,即可用大小 * * @return */ private String getSDAvailableSize() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return Formatter.formatFileSize(this, blockSize * availableBlocks); }
手机系统的内存也可以通过ActivityManager.getMemoryInfo()方法获取:如下:
获取系统的内存大小以及可用内存大小:
/** * 获取系统内存大小 * @return */ private String getSysteTotalMemorySize(){ //获得ActivityManager服务的对象 ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); //获得MemoryInfo对象 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo() ; //获得系统可用内存,保存在MemoryInfo对象上 mActivityManager.getMemoryInfo(memoryInfo) ; long memSize = memoryInfo.totalMem ; //字符类型转换 String availMemStr = formateFileSize(memSize); return availMemStr ; } /** * 获取系统可用的内存大小 * @return */ private String getSystemAvaialbeMemorySize(){ //获得ActivityManager服务的对象 ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); //获得MemoryInfo对象 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo() ; //获得系统可用内存,保存在MemoryInfo对象上 mActivityManager.getMemoryInfo(memoryInfo) ; long memSize = memoryInfo.availMem ; //字符类型转换 String availMemStr = formateFileSize(memSize); return availMemStr ; }