The Yaroslavl answer will give the size of an SD card, not the available space. StatFs getAvailableBlocks() will return the number of blocks that are still available for regular programs. Here is the function I'm 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 a link to some deprecated features as of August 13, 2014. Below I reproduce the updated version:
public static float megabytesAvailable(File f) { StatFs stat = new StatFs(f.getPath()); long bytesAvailable = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) bytesAvailable = (long) stat.getBlockSizeLong() * (long) stat.getAvailableBlocksLong(); else bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks(); return bytesAvailable / (1024.f * 1024.f); }
user79758 Apr 21 '11 at 11:30 2011-04-21 11:30
source share