How can I programmatically check how much free space an SD card mounted on an Android device has?
Asked
Active
Viewed 2.7k times
5 Answers
80
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
-
this 1 is sooo helpful. the other one is a tease. – murftown Dec 06 '11 at 14:13
-
On SamsungGalaxyS with android 2.3.4 result is far far away from truth. This code gieves me 1,089GB. But device report for 218mega free on sd card, 5.06gb on usb and 1.34 on device – Kostadin Jun 04 '12 at 11:27
-
11`getAvailableBlocks()` and `getBlockSize()` are both **deprecated in API 18** so I would do some kind of check on your build version to make sure you are applying the correct methods: `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){ @SuppressWarnings("deprecation") long sdAvailSize = (long)stat.getAvailableBlocksLong() * (long)stat.getBlockSizeLong(); } else{ @SuppressWarnings("deprecation") double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize(); }` – Jordan Hochstetler Sep 28 '13 at 23:37
-
but now its a deprecated what is new instance of this. plz help me coz some time it will not work in my app – Bhanu Sharma Dec 04 '13 at 09:00
-
I am trying to use this method in my app. No problems for me (getting path differently), but on a friend's phone internal is giving me problems. `@SuppressLint("NewApi")public static long getTotalSpace(String path) {StatFs statFs = new StatFs(path);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {long sdTotalSize=statFs.getBlockCountLong()*statFs.getBlockSizeLong();return sdTotalSize;} else {@SuppressWarnings("deprecation")double sdTotalSize = (double)statFs.getBlockCount() *statFs.getBlockSize(); return (long) sdTotalSize;}` It's showing total above < `statFs.getFreeBlocks()` – MattMatt Jan 23 '14 at 07:57
-
1this returns the available space of internal memory not the SD card memory – Mr.G Oct 28 '14 at 15:05
13
/**
* 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;
}
}

Muhammad Nabeel Arif
- 19,140
- 8
- 51
- 70
-
Can I ask why the ".restat(...)"? Also I believe that you made a mistake using Environment.getDataDirectory().getPath() in "getExternalAvailableSpaceInBytes()" instead of Environment.getExternalStorageDirectory().getPath()... – Kasium Apr 23 '12 at 10:15
-
Fixed: You are right in both issues, restat was not needed as the StatFs object is created at the spot and has latest information. And data directory path was mistakenly added. thanks. – Muhammad Nabeel Arif Apr 23 '12 at 11:19
11
Here's a simpler way, that is usable since API level 9:
Environment.getExternalStorageDirectory().getUsableSpace();
http://developer.android.com/reference/java/io/File.html#getUsableSpace()

tknell
- 9,007
- 3
- 24
- 28
2
This code is working for me:
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;

Nuno Veloso
- 21
- 4
0
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);
}

josliber
- 43,891
- 12
- 98
- 133

Javid Javidi
- 1
- 1