Как узнать объем свободной и занятой памяти Андроид устройства, то есть вывести это в переменную. Java, Android.
1 ответ
Вот есть способ:
@RequiresApi(api = Build.VERSION_CODES.O)
private void showStorageVolumes() {
StorageStatsManager storageStatsManager = (StorageStatsManager) getSystemService(Context.STORAGE_STATS_SERVICE);
StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
if (storageManager == null || storageStatsManager == null) {
return;
}
List<StorageVolume> storageVolumes = storageManager.getStorageVolumes();
for (StorageVolume storageVolume : storageVolumes) {
final String uuidStr = storageVolume.getUuid();
final UUID uuid = uuidStr == null ? StorageManager.UUID_DEFAULT : UUID.fromString(uuidStr);
try {
Log.d("AppLog", "storage:" + uuid + " : " + storageVolume.getDescription(this) + " : " + storageVolume.getState());
Log.d("AppLog", "getFreeBytes:" + Formatter.formatShortFileSize(this, storageStatsManager.getFreeBytes(uuid)));
Log.d("AppLog", "getTotalBytes:" + Formatter.formatShortFileSize(this, storageStatsManager.getTotalBytes(uuid)));
} catch (Exception e) {
// IGNORED
}
}
}
вот ссылка на первоисточник. В этом способе все в логи идет, но можно перенести в переменные которые вам нужно. Если что будет непонятно, не стесняйтесь и спрашивайте, постараемся помочь. Удачи :)