Android Tutorial: Calculate Available Space Of SD Card

 ·  · 

Tutorial of calculating available free space of Android SD card.

Basic concepts:

  1. Get the block size of SD card storage (in bytes);
  2. Get the number of available blocks;
  3. Multiply block size by the number of available blocks, get the available space size (in bytes);
  4. Format the size using user friendly units, e.g. MB, GB;
import android.os.StatFs;
import android.text.format.Formatter;
import android.os.Environment;
import android.widget.Toast;

private void calculateAvailableSpace() {
    File file = Environment.getExternalStorageDirectory();
    StatFs sdCardStat = new StatFs(file.getPath());
    long blockSize;
    long totalBlocks;
    long availableBlocks;

    // Android 4.3+
        blockSize = sdCardStat.getBlockSizeLong();
        totalBlocks = sdCardStat.getBlockCountLong();
        availableBlocks = sdCardStat.getAvailableBlocksLong();
    } else {
        blockSize = sdCardStat.getBlockSize();
        totalBlocks = sdCardStat.getBlockCount();
        availableBlocks = sdCardStat.getAvailableBlocks();

    long totalSize = blockSize * totalBlocks;
    long availableSize = blockSize * availableBlocks;

    String total = Formatter.formatFileSize(this, totalSize);
    String available = Formatter.formatFileSize(this, availableSize);

    Toast.makeText(this, available + "/" + total, Toast.LENGTH_LONG).show();


  1. Note that the getBlockSize() API was deprecated in Android 4.3 (JELLYBEANMR2). We should use getBlockSizeLong() instead in Android 4.3+. The same rule applies to getBlockCount(), getAvailableBlocks();
  2. We checked Android API level by using Build.VERSION.SDK_INT;