直接记录方法下来,我个人理解  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

// 获取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类,用反射获得,从类中可取得内存大小

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐