How to find out the amount of free and used memory of an Android device, that is, output it to a variable. Java, Android.

    1 answer 1

    Here is a way:

    @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 } } } 

    here is the link to the source. In this method, everything goes to logs, but can be transferred to variables that you need. If anything is unclear, do not hesitate and ask, we will try to help. Good luck :)