在Android中,可以通过以下方式读取RAM、ROM和SD卡的容量:
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long totalRam = memoryInfo.totalMem;
long availableRam = memoryInfo.availMem;
File externalStorage = Environment.getExternalStorageDirectory();
long totalRom = externalStorage.getTotalSpace();
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File externalStorage = Environment.getExternalStorageDirectory();
long totalSdCard = externalStorage.getTotalSpace();
}
需要注意的是,读取SD卡容量时需要添加读取外部存储的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />