34

How can I programmatically check how much free space an SD card mounted on an Android device has?

5 Answers 5

80

To get the external SD card's available "free" space to show a number which agrees with the Menu->Settings->SD card and phone storage's number, use the following code:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
double sdAvailSize = (double)stat.getAvailableBlocks()
                   * (double)stat.getBlockSize();
//One binary gigabyte equals 1,073,741,824 bytes.
double gigaAvailable = sdAvailSize / 1073741824;

Relevant documentation: http://developer.android.com/reference/android/os/StatFs.html

6
  • this 1 is sooo helpful. the other one is a tease.
    – murftown
    Dec 6, 2011 at 14:13
  • On SamsungGalaxyS with android 2.3.4 result is far far away from truth. This code gieves me 1,089GB. But device report for 218mega free on sd card, 5.06gb on usb and 1.34 on device
    – Kostadin
    Jun 4, 2012 at 11:27
  • 11
    getAvailableBlocks() and getBlockSize() are both deprecated in API 18 so I would do some kind of check on your build version to make sure you are applying the correct methods: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){ @SuppressWarnings("deprecation") long sdAvailSize = (long)stat.getAvailableBlocksLong() * (long)stat.getBlockSizeLong(); } else{ @SuppressWarnings("deprecation") double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize(); } Sep 28, 2013 at 23:37
  • but now its a deprecated what is new instance of this. plz help me coz some time it will not work in my app Dec 4, 2013 at 9:00
  • 1
    this returns the available space of internal memory not the SD card memory
    – Mr.G
    Oct 28, 2014 at 15:05
13
/**
 * This class is designed to get available space in external storage of android. 
 * It contains methods which provide you the available space in different units e.g
 * bytes, KB, MB, GB. OR you can get the number of available blocks on external storage.
 *
 */
public class AvailableSpaceHandler {

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

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

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

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

    /**
     * @return Number of bytes available on external storage
     */
    public static long getExternalAvailableSpaceInBytes() {
        long availableSpace = -1L;
        try {
            StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
            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(){
        return getExternalAvailableSpaceInBytes()/SIZE_KB;
    }
    /**
     * @return Number of Mega bytes available on external storage
     */
    public static long getExternalAvailableSpaceInMB(){
        return getExternalAvailableSpaceInBytes()/SIZE_MB;
    }

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

    /**
     * @return Total number of available blocks on external storage
     */
    public static long getExternalStorageAvailableBlocks() {
        long availableBlocks = -1L;
        try {
            StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
            availableBlocks = stat.getAvailableBlocks();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return availableBlocks;
    }
}
2
  • Can I ask why the ".restat(...)"? Also I believe that you made a mistake using Environment.getDataDirectory().getPath() in "getExternalAvailableSpaceInBytes()" instead of Environment.getExternalStorageDirectory().getPath()...
    – Kasium
    Apr 23, 2012 at 10:15
  • Fixed: You are right in both issues, restat was not needed as the StatFs object is created at the spot and has latest information. And data directory path was mistakenly added. thanks. Apr 23, 2012 at 11:19
11

Here's a simpler way, that is usable since API level 9:

Environment.getExternalStorageDirectory().getUsableSpace();

http://developer.android.com/reference/java/io/File.html#getUsableSpace()

0
2

This code is working for me:

StatFs stat = new StatFs(System.getenv("SECONDARY_STORAGE"));

long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();

long kiloAvailable = bytesAvailable / 1024; // Available space from SD in KB
long megaAvailable = bytesAvailable / (1024*1024); // Available space from SD in MB

return (int)kiloAvailable; 
0
public static float megabytesAvailable(String  StrPath) {
  StatFs stat = new StatFs(StrPath);

  long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
  return bytesAvailable / (1024.f * 1024.f);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.