0

How can I get free space available in Internal and External storages? I'm developing an app that downloads a ROM and I have to check if there is enough free space to download it. Can anyone help me? Thanks in advance!

peppe130
  • 1
  • 1

2 Answers2

1

According to user79758:

Here is the function I am using:

public static float megabytesAvailable(File f) {
    StatFs stat = new StatFs(f.getPath());
    long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
    return bytesAvailable / (1024.f * 1024.f);
}

The above code has reference to some deprecated functions as of August 13, 2014. I below reproduce an updated version:

public static float megabytesAvailable(File f) {
    StatFs stat = new StatFs(f.getPath());
    long bytesAvailable = (long)stat.getBlockSizeLong() * (long)stat.getAvailableBlocksLong();
    return bytesAvailable / (1024.f * 1024.f);
}
Nirel
  • 1,855
  • 1
  • 15
  • 26
0

This is one way you can check memory status in android. I have implemented in my application.

public class AvailableSpaceHandler {

    //*********
    //Variables
    /**
     * Number of bytes in one KB = 2<sup>10</sup>
     */
    private final static long SIZE_KB = 1024L;

    /**
     * Number of bytes in one MB = 2<sup>20</sup>
     */
    private final static long SIZE_MB = SIZE_KB * SIZE_KB;

    /**
     * Number of bytes in one GB = 2<sup>30</sup>
     */
    private final static long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;

    //********
    // Methods

    /**
     * @return Number of bytes available on external storage
     */
    private static long getExternalAvailableSpaceInBytes(int newAndroidAPI) {
        long availableSpace = -1L;
        try {
            StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
            if (newAndroidAPI >= Build.VERSION_CODES.KITKAT) {
                availableSpace = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
            } else {
                availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return availableSpace;
    }


    /**
     * @return Number of kilo bytes available on external storage
     */
    public static long getExternalAvailableSpaceInKB(int newAndroidAPI) {
        return getExternalAvailableSpaceInBytes(newAndroidAPI) / SIZE_KB;
    }

    /**
     * @return Number of Mega bytes available on external storage
     */
    public static long getExternalAvailableSpaceInMB(int newAndroidAPI) {
        return getExternalAvailableSpaceInBytes(newAndroidAPI) / SIZE_MB;
    }

    /**
     * @return gega bytes of bytes available on external storage
     */
    public static long getExternalAvailableSpaceInGB(int newAndroidAPI) {
        return getExternalAvailableSpaceInBytes(newAndroidAPI) / SIZE_GB;
    }

    /**
     * @return Total number of available blocks on external storage
     */
    public static long getExternalStorageAvailableBlocks(int newAndroidAPI) {
        //newAndroidAPI to check API is < KITKAT
        long availableBlocks = -1L;
        try {
            StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
            if (newAndroidAPI >= Build.VERSION_CODES.KITKAT) {
                availableBlocks = stat.getAvailableBlocksLong();
            } else {
                availableBlocks = stat.getAvailableBlocks();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return availableBlocks;
    }
}
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142