Android5.1sdk22版本获取系统内存大小代码
查看Android不同版本API的地址为:
https://developer.android.google.cn/reference/packages
选择对应的Android版本,查看对应的Android模块,即可查找到对应版本的API参考文档:
//计算EMMC和DDR TextView txEmmcTotal = v.findViewById(R.id.txEmmcTotal); TextView txDDRTotal = v.findViewById(R.id.txDDRTotal); TextView txEmmcRest = v.findViewById(R.id.txEmmcRest); TextView txDDRRest = v.findViewById(R.id.txDDRRest); ActivityManager activityManager = (ActivityManager) requireActivity().getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); float totalMemory = (float)memoryInfo.totalMem/(1024 * 1024 * 1024); Log.e(TAG, "setVersionInfo: totalMemory= "+ String.format("%.2f",totalMemory) + 'G'); txEmmcTotal.setText(String.format("%.2f",totalMemory) + "GB"); float availMemory = (float)memoryInfo.availMem/(1024*1024*1024); Log.e(TAG, "setVersionInfo: availMemory= "+ String.format("%.2f",availMemory) + "GB"); txEmmcRest.setText(String.format("%.2f",availMemory) + "GB"); //系统文件大小 StatFs stat = new StatFs(Environment.getRootDirectory().getAbsolutePath()); long systemSize = stat.getTotalBytes(); Log.e(TAG, "setVersionInfo: systemSize= "+ String.format("%.2f",systemSize/(1024f * 1024 * 1024)) + "GB"); txDDRTotal.setText(String.format("%.2f",systemSize/(1024f * 1024 * 1024)) + "GB"); long availableSize = stat.getAvailableBytes(); Log.e(TAG, "setVersionInfo: availableSize="+ String.format("%.2f",availableSize/(1024f * 1024 * 1024)) + "GB"); txDDRRest.setText(String.format("%.2f",availableSize/(1024f * 1024 * 1024)) + "GB"); //内部sd存储空间大小 // StatFs stat1 = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); // Log.e(TAG, "setVersionInfo: getExternalStorageDirectory="+Environment.getExternalStorageDirectory().getAbsolutePath()); // long internal_sd_totalBlocks = stat1.getBlockCountLong();// 获取block数量 // long internal_sd_blockSize = stat1.getBlockSizeLong(); // long internal_sd_totalSize = internal_sd_blockSize * internal_sd_totalBlocks; // Log.e(TAG, "setVersionInfo: internal_sd_totalSize="+ String.format("%.2f",internal_sd_totalSize/(1024f * 1024 * 1024)) + "GB");
0条评论