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();
}
StatFs class
you can use here, provide the path for your internal and external directory and calculate the total, free and avialable space.
StatFs memStatus = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)memStatus.getBlockSize() * (long)memStatus.getAvailableBlocks();
See the documentation for more details. bytesAvailable
is in bytes you can convert it in to which ever format you want.
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;
For Android 4.3+ (API Level: 18+)
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = getAvailableBytes();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With