How to find the amount of free storage (disk space) left on Android?

Example: Getting human readable size like 1 Gb

String memory = bytesToHuman(totalMemory())

/*************************************************************************************************
Returns size in bytes.

If you need calculate external memory, change this: 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
**************************************************************************************************/
    public long totalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize());
        return total;
    }

    public long freeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize());
        return free;
    }

    public long busyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize());
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize());
        long   busy   = total - free;
        return busy;
    }

Converting bytes to human readable format (like 1 Mb, 1 Gb)

    public static String floatForm (double d)
    {
       return new DecimalFormat("#.##").format(d);
    }


    public static String bytesToHuman (long size)
    {
        long Kb = 1  * 1024;
        long Mb = Kb * 1024;
        long Gb = Mb * 1024;
        long Tb = Gb * 1024;
        long Pb = Tb * 1024;
        long Eb = Pb * 1024;

        if (size <  Kb)                 return floatForm(        size     ) + " byte";
        if (size >= Kb && size < Mb)    return floatForm((double)size / Kb) + " Kb";
        if (size >= Mb && size < Gb)    return floatForm((double)size / Mb) + " Mb";
        if (size >= Gb && size < Tb)    return floatForm((double)size / Gb) + " Gb";
        if (size >= Tb && size < Pb)    return floatForm((double)size / Tb) + " Tb";
        if (size >= Pb && size < Eb)    return floatForm((double)size / Pb) + " Pb";
        if (size >= Eb)                 return floatForm((double)size / Eb) + " Eb";

        return "???";
    }

Try StatFs.getAvailableBlocks. You'll need to convert the block count to KB with getBlockSize.


Based on @XXX's answer, I've created a gist code snippet that wraps StatFs for easy and simple usage. You can find it here as a GitHub gist.


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:

  1. 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.

  2. 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.

  3. 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.

  4. 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:

// Get internal (data partition) free space
// This will match what's shown in System Settings > Storage for 
// Internal Space, when you subtract Total - Used
public long getFreeInternalMemory()
{
    return getFreeMemory(Environment.getDataDirectory());
}

// Get external (SDCARD) free space
public long getFreeExternalMemory()
{
    return getFreeMemory(Environment.getExternalStorageDirectory());
}

// Get Android OS (system partition) free space
public long getFreeSystemMemory()
{
    return getFreeMemory(Environment.getRootDirectory());
}

// Get free space for provided path
// Note that this will throw IllegalArgumentException for invalid paths
public long getFreeMemory(File path)
{
    StatFs stats = new StatFs(path.getAbsolutePath());
    return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
}