我设计了一些现成的函数来获取不同单位的可用空间。您可以通过简单地将其中任何一种复制到项目中来使用这些方法。/** * @return Number of bytes available on External storage */public static long getAvailableSpaceInBytes() { long availableSpace = -1L; StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); return availableSpace;}/** * @return Number of kilo bytes available on External storage */public static long getAvailableSpaceInKB(){ final long SIZE_KB = 1024L; long availableSpace = -1L; StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); return availableSpace/SIZE_KB;}/** * @return Number of Mega bytes available on External storage */public static long getAvailableSpaceInMB(){ final long SIZE_KB = 1024L; final long SIZE_MB = SIZE_KB * SIZE_KB; long availableSpace = -1L; StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); return availableSpace/SIZE_MB;}/** * @return Number of gega bytes available on External storage */public static long getAvailableSpaceInGB(){ final long SIZE_KB = 1024L; final long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB; long availableSpace = -1L; StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); return availableSpace/SIZE_GB;}