Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If There Is Sufficient Memory On SD Card Programmatically

Tags:

java

android

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();
}
like image 869
Naskov Avatar asked Apr 10 '13 11:04

Naskov


3 Answers

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.

like image 197
Triode Avatar answered Nov 05 '22 17:11

Triode


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;
like image 4
Shrikant Ballal Avatar answered Nov 05 '22 17:11

Shrikant Ballal


For Android 4.3+ (API Level: 18+)

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = getAvailableBytes();
like image 1
stevo.mit Avatar answered Nov 05 '22 15:11

stevo.mit