Check If There Is Sufficient Memory On SD Card Programmatically

android, java

Solution

from: http://groups.google.com/group/android-developers/browse_thread/thread/ecede996463a4058

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;

Problem

My app is saving files on the SD Card but before I save the files I need to check if there is free memory. I need a to check how much free memory is on the SD Card. Something like: ``` if(MemoryCard.getFreeMemory()>20Mb) { saveFiles(); } else { Toast.makeText(this, "Not enough memory", 100).show(); } ```

Original source