title: Android get free size of internal/external memory
body: <p>I want to get the size of free memory on internal/external storage of my device programmatically. I'm using this piece of code :</p>
  
  <pre><code>StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
  long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
  long megAvailable = bytesAvailable / 1048576;
  Log.e("","Available MB : "+megAvailable);
  
  File path = Environment.getDataDirectory();
  StatFs stat2 = new StatFs(path.getPath());
  long blockSize = stat2.getBlockSize();
  long availableBlocks = stat2.getAvailableBlocks();
  String format =  Formatter.formatFileSize(this, availableBlocks * blockSize);
  Log.e("","Format : "+format);
  </code></pre>
  
  <p>and the result which I'm getting is :</p>
  
  <pre><code>11-15 10:27:18.844: E/(25822): Available MB : 7572
  11-15 10:27:18.844: E/(25822): Format : 869MB
  </code></pre>
  
  <p>The problem is that I want to get the free memory of SdCard which is <code>1,96GB</code> right now. How can I fix this code so I can get the free size ?</p>