¿Cómo puedo verificar cuánto espacio libre tiene una tarjeta SD montada en un dispositivo Android?

¿Cómo puedo verificar programáticamente cuánto espacio libre tiene una tarjeta SD montada en un dispositivo Android?

Para obtener el espacio “libre” disponible de la tarjeta SD externa para mostrar un número que concuerde con el Menú-> Configuración-> tarjeta SD y el número de almacenamiento del teléfono, use el siguiente código:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize(); //One binary gigabyte equals 1,073,741,824 bytes. double gigaAvailable = sdAvailSize / 1073741824; 

Documentación relevante: http://developer.android.com/reference/android/os/StatFs.html

 /** * This class is designed to get available space in external storage of android. * It contains methods which provide you the available space in different units eg * bytes, KB, MB, GB. OR you can get the number of available blocks on external storage. * */ public class AvailableSpaceHandler { //********* //Variables /** * Number of bytes in one KB = 210 */ public final static long SIZE_KB = 1024L; /** * Number of bytes in one MB = 220 */ public final static long SIZE_MB = SIZE_KB * SIZE_KB; /** * Number of bytes in one GB = 230 */ public final static long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB; //******** // Methods /** * @return Number of bytes available on external storage */ public static long getExternalAvailableSpaceInBytes() { long availableSpace = -1L; try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); } catch (Exception e) { e.printStackTrace(); } return availableSpace; } /** * @return Number of kilo bytes available on external storage */ public static long getExternalAvailableSpaceInKB(){ return getExternalAvailableSpaceInBytes()/SIZE_KB; } /** * @return Number of Mega bytes available on external storage */ public static long getExternalAvailableSpaceInMB(){ return getExternalAvailableSpaceInBytes()/SIZE_MB; } /** * @return gega bytes of bytes available on external storage */ public static long getExternalAvailableSpaceInGB(){ return getExternalAvailableSpaceInBytes()/SIZE_GB; } /** * @return Total number of available blocks on external storage */ public static long getExternalStorageAvailableBlocks() { long availableBlocks = -1L; try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); availableBlocks = stat.getAvailableBlocks(); } catch (Exception e) { e.printStackTrace(); } return availableBlocks; } } 

Aquí hay una manera más simple, que se puede utilizar desde API nivel 9:

 Environment.getExternalStorageDirectory().getUsableSpace(); 

http://developer.android.com/reference/java/io/File.html#getUsableSpace ()

Este código me funciona:

 StatFs stat = new StatFs(System.getenv("SECONDARY_STORAGE")); long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks(); long kiloAvailable = bytesAvailable / 1024; // Available space from SD in KB long megaAvailable = bytesAvailable / (1024*1024); // Available space from SD in MB return (int)kiloAvailable; 
 public static float megabytesAvailable(String StrPath) { StatFs stat = new StatFs(StrPath); long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks(); return bytesAvailable / (1024.f * 1024.f); }