Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check how much free space an SD card mounted on an Android device has?

Tags:

android

How can I programmatically check how much free space an SD card mounted on an Android device has?

like image 349
Andy Avatar asked May 31 '10 05:05

Andy


People also ask

How can I see what's taking up space on my SD card?

To see what those files are, go to Settings > Storage > Internal Storage. Whatever is taking up the most storage space will appear on top, and it'll show you how much storage it's taking up. If you want to view the pictures or files, you only have to tap on them.


2 Answers

To get the external SD card's available "free" space to show a number which agrees with the Menu->Settings->SD card and phone storage's number, use the following code:

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; 

Relevant documentation: http://developer.android.com/reference/android/os/StatFs.html

like image 158
Rick Avatar answered Oct 05 '22 23:10

Rick


/**  * 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 e.g  * 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 = 2<sup>10</sup>      */     public final static long SIZE_KB = 1024L;      /**      * Number of bytes in one MB = 2<sup>20</sup>      */     public final static long SIZE_MB = SIZE_KB * SIZE_KB;      /**      * Number of bytes in one GB = 2<sup>30</sup>      */     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;     } } 
like image 36
Muhammad Nabeel Arif Avatar answered Oct 05 '22 23:10

Muhammad Nabeel Arif