Check if there is enough memory on the SD card on the memory card

My application saves files on an SD card, but before saving files, I need to check if there is free memory. I need to check how free memory is on the SD card.

Sort of:

if(MemoryCard.getFreeMemory()>20Mb) { saveFiles(); } else { Toast.makeText(this, "Not enough memory", 100).show(); } 
+6
source share
3 answers

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; 
+4
source
 StatFs class 

you can use here, specify the path for your internal and external directory and calculate the total, free and available 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 to the format you need.

+7
source

For Android 4.3+ (API level: 18+)

 StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); long bytesAvailable = getAvailableBytes(); 
+1
source

All Articles