How to check the available space on an Android device? on the SD card?

How to check how many MB or GB are left on an Android device? I am using JAVA and Android SDK 2.0.1.

Is there any system service that can expose something like this?

+42
java android
Aug 03 '10 at 8:18
source share
9 answers

Try this code

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount(); long megAvailable = bytesAvailable / 1048576; System.out.println("Megs :"+megAvailable); 

From there

UPD: getBlockCount () - returns the size of the SD card; getAvailableBlocks () - returns the number of blocks that are still available for regular programs (thanks Joe)

+50
Aug 03 '10 at 8:57
source share

The Yaroslavl answer will give the size of an SD card, not the available space. StatFs getAvailableBlocks() will return the number of blocks that are still available for regular programs. Here is the function I'm using:

 public static float megabytesAvailable(File f) { StatFs stat = new StatFs(f.getPath()); long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks(); return bytesAvailable / (1024.f * 1024.f); } 

The above code has a link to some deprecated features as of August 13, 2014. Below I reproduce the updated version:

 public static float megabytesAvailable(File f) { StatFs stat = new StatFs(f.getPath()); long bytesAvailable = 0; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) bytesAvailable = (long) stat.getBlockSizeLong() * (long) stat.getAvailableBlocksLong(); else bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks(); return bytesAvailable / (1024.f * 1024.f); } 
+68
Apr 21 '11 at 11:30
source share

I developed some ready-to-use functions to get the available space in different units. You can use these methods simply by copying any of them into your project.

 /** * @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; } 
+16
Apr 03 '12 at 8:10
source share

New methods have been introduced with API version 18.

I used something like this to estimate the size of a large cache drive (for the Picasso OkHttp bootloader cache). The helper method was this:

 private static final String BIG_CACHE_PATH = "my-cache-dir"; private static final float MAX_AVAILABLE_SPACE_USE_FRACTION = 0.9f; private static final float MAX_TOTAL_SPACE_USE_FRACTION = 0.25f; static File createDefaultCacheDirExample(Context context) { File cache = new File(context.getApplicationContext().getCacheDir(), BIG_CACHE_PATH); if (!cache.exists()) { cache.mkdirs(); } return cache; } /** * Calculates minimum of available or total fraction of disk space * * @param dir * @return space in bytes */ @SuppressLint("NewApi") static long calculateAvailableCacheSize(File dir) { long size = 0; try { StatFs statFs = new StatFs(dir.getAbsolutePath()); int sdkInt = Build.VERSION.SDK_INT; long totalBytes; long availableBytes; if (sdkInt < Build.VERSION_CODES.JELLY_BEAN_MR2) { int blockSize = statFs.getBlockSize(); availableBytes = ((long) statFs.getAvailableBlocks()) * blockSize; totalBytes = ((long) statFs.getBlockCount()) * blockSize; } else { availableBytes = statFs.getAvailableBytes(); totalBytes = statFs.getTotalBytes(); } // Target at least 90% of available or 25% of total space size = (long) Math.min(availableBytes * MAX_AVAILABLE_SPACE_USE_FRACTION, totalBytes * MAX_TOTAL_SPACE_USE_FRACTION); } catch (IllegalArgumentException ignored) { // ignored } return size; } 
+4
Apr 14 '14 at 14:19
source share

Based on this answer, Added support for Android version <18

 public static float megabytesAvailable(File file) { StatFs stat = new StatFs(file.getPath()); long bytesAvailable; if(Build.VERSION.SDK_INT >= 18){ bytesAvailable = getAvailableBytes(stat); } else{ //noinspection deprecation bytesAvailable = stat.getBlockSize() * stat.getAvailableBlocks(); } return bytesAvailable / (1024.f * 1024.f); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static long getAvailableBytes(StatFs stat) { return stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); } 
+4
Aug 13 '15 at 10:08
source share

also, if you want to check the available space in the internal memory, use:

 File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); 

...

+3
Feb 08 2018-12-12T00:
source share

Google has information on this on the getting started page - see Query Free Space. They say that you can check the available space on getFreeSpace() , but they claim it is inaccurate and you should expect a little less free space than that. They say:

If the return number is a few MB larger than the size of the data you want to keep, or if the file system is 90% full, then it is probably safe to continue. Otherwise, you probably should not record for storage.

They also give tips that it is often useful not to check the free space at all and just try catch for an error:

You do not need to check the amount of available space before saving the file. Instead, you can try to write the file right away, and then catch an IOException if that happens. You may need to do this if you do not know how much space you need. For example, if you changed the encoding of a file before saving it, converting a PNG image to JPEG, you will not know the file size in advance.

I would recommend that only for very large file sizes you should check the available storage in advance so as not to lose the download or creation time of the file, which, if obviously too large to hold. In any case, you should always use try catch , so I believe that the only argument for checking the available free space in advance is if it is either an unnecessary use of resources and there is too much time.

+1
Jun 10 '15 at 7:06
source share

Hope this code helps others. Tested. Thanks to clarify it.

 /** * Get the free disk available space in boolean to download requested file * * @return boolean value according to size availability */ protected static boolean isMemorySizeAvailableAndroid(long download_bytes, boolean isExternalMemory) { boolean isMemoryAvailable = false; long freeSpace = 0; // if isExternalMemory get true to calculate external SD card available size if(isExternalMemory){ try { StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} }else{ // find phone available size try { StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); freeSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); if(freeSpace > download_bytes){ isMemoryAvailable = true; }else{ isMemoryAvailable = false; } } catch (Exception e) {e.printStackTrace(); isMemoryAvailable = false;} } return isMemoryAvailable; } 
0
Jan 02 '13 at 16:01
source share
 public String TotalExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Total); return strI; } public String FreeExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; String strI = Integer.toString(Free); return strI; } public String BusyExtMemory() { StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576; int Free = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576; int Busy = Total - Free; String strI = Integer.toString(Busy); return strI; } 
-one
Jan 04 '13 at
source share



All Articles