在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" />