I am trying to figure out the available disk space on the Android phone running my application. Is there a way to do this programmatically?
Thanks,
I am trying to figure out the available disk space on the Android phone running my application. Is there a way to do this programmatically?
Thanks,
Since blocksize and getAvailableBlocks
are deprecated
this code can be use
note based above answer by user802467
we can use
getAvailableBlocksLong
andgetBlockSizeLong
With a little google you might had found the
StatFs
-class which is:Examples are provided
There are some subtleties regarding paths that none of the current answers address. You must use the right path based on what kind of stats you are interested in. Based on a deep dive into DeviceStorageMonitorService.java which generates the low disk space warnings in the notification area and the sticky broadcasts for ACTION_DEVICE_STORAGE_LOW, here are some of the paths that you can use:
To check free internal disk space use the data directory obtained via Environment.getDataDirectory(). This will get you the free space on the data partition. The data partition hosts all the internal storage for all apps on the device.
To check free external (SDCARD) disk space use the external storage directory obtained via Environment.getExternalStorageDirectory(). This will get you the free space on the SDCARD.
To check for available memory on the system partition which contains OS files, use Environment.getRootDirectory(). Since your app has no access to the system partition, this stat is probably not very useful. DeviceStorageMonitorService uses for informational purposes and enters it into a log.
To check for temporary files / cache memory, use Environment.getDownloadCacheDirectory(). DeviceStorageMonitorService attempts to clean some of the temporary files when memory gets low.
Some sample code for getting the internal (/data), external (/sdcard) and OS (/system) free memory:
Memory Locations:
usage
You can use this
Example: Getting human readable size like 1 Gb
String memory = bytesToHuman(totalMemory())
Converting bytes to human readable format (like 1 Mb, 1 Gb)