How to find the amount of free storage (disk space) left on Android?

I am trying to figure out the available disk space on the Android phone running my application. Is there a way to do this programmatically?

Thanks,

  • Is it possible to generate a 'share on Facebook' link that opens the native Facebook App on Android/iOS/mobile instead of the web share dialog?
  • Page scroll when soft keyboard poped up
  • Is using registerReceiver on the application class cosidered a good, known practice?
  • Android YouTubePlayer with unauthorized overlay on top of player
  • Make a wizard like application in Android
  • Android ScrollView Vs ListView
  • Related posts:

    Android Facebook style slide
    Event when the FragmentTransaction is completed
    No implementation found for long org.opencv.core.Mat.n_Mat() error Using OpenCV
    How to detect if any of my activity is front-most and visible to user?
    PagerTabStrip position within ViewPager
    how can i get the value of a color resource in my activity
  • how to resolve this error “com.android.internal.telephony cannot be resolved to a type” in android
  • Escape “ ” in Text in TextView
  • getActivity() in fragment is null after orientation change
  • Volley requests not working with Django Sessions
  • How to use different background images for different resolutions in Android?
  • YouTube ads never show while using YouTube Android Player API
  • 7 Solutions collect form web for “How to find the amount of free storage (disk space) left on Android?”

    Try StatFs.getAvailableBlocks. You’ll need to convert the block count to KB with getBlockSize.

    Example: Getting human readable size like 1 Gb

    String memory = bytesToHuman(totalMemory())

    /*************************************************************************************************
    Returns size in bytes.
    
    If you need calculate external memory, change this: 
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    to this: 
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
    **************************************************************************************************/
        public long totalMemory()
        {
            StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
            long   total  = (statFs.getBlockCount() * statFs.getBlockSize());
            return total;
        }
    
        public long freeMemory()
        {
            StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
            long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize());
            return free;
        }
    
        public long busyMemory()
        {
            StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
            long   total  = (statFs.getBlockCount() * statFs.getBlockSize());
            long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize());
            long   busy   = total - free;
            return busy;
        }
    

    Converting bytes to human readable format (like 1 Mb, 1 Gb)

        public static String floatForm (double d)
        {
           return new DecimalFormat("#.##").format(d);
        }
    
    
        public static String bytesToHuman (long size)
        {
            long Kb = 1  * 1024;
            long Mb = Kb * 1024;
            long Gb = Mb * 1024;
            long Tb = Gb * 1024;
            long Pb = Tb * 1024;
            long Eb = Pb * 1024;
    
            if (size <  Kb)                 return floatForm(        size     ) + " byte";
            if (size >= Kb && size < Mb)    return floatForm((double)size / Kb) + " Kb";
            if (size >= Mb && size < Gb)    return floatForm((double)size / Mb) + " Mb";
            if (size >= Gb && size < Tb)    return floatForm((double)size / Gb) + " Gb";
            if (size >= Tb && size < Pb)    return floatForm((double)size / Tb) + " Tb";
            if (size >= Pb && size < Eb)    return floatForm((double)size / Pb) + " Pb";
            if (size >= Eb)                 return floatForm((double)size / Eb) + " Eb";
    
            return "???";
        }
    

    Based on @XXX’s answer, I’ve created a gist code snippet that wraps StatFs for easy and simple usage.
    You can find it here as a GitHub gist.

    There are some subtleties regarding paths that none of the current answers address. You must use the right path based on what kind of stats you are interested in. Based on a deep dive into DeviceStorageMonitorService.java which generates the low disk space warnings in the notification area and the sticky broadcasts for ACTION_DEVICE_STORAGE_LOW, here are some of the paths that you can use:

    1. To check free internal disk space use the data directory obtained via Environment.getDataDirectory(). This will get you the free space on the data partition. The data partition hosts all the internal storage for all apps on the device.

    2. To check free external (SDCARD) disk space use the external storage directory obtained via Environment.getExternalStorageDirectory(). This will get you the free space on the SDCARD.

    3. To check for available memory on the system partition which contains OS files, use Environment.getRootDirectory(). Since your app has no access to the system partition, this stat is probably not very useful. DeviceStorageMonitorService uses for informational purposes and enters it into a log.

    4. To check for temporary files / cache memory, use Environment.getDownloadCacheDirectory(). DeviceStorageMonitorService attempts to clean some of the temporary files when memory gets low.

    Some sample code for getting the internal (/data), external (/sdcard) and OS (/system) free memory:

    // Get internal (data partition) free space
    // This will match what's shown in System Settings > Storage for 
    // Internal Space, when you subtract Total - Used
    public long getFreeInternalMemory()
    {
        return getFreeMemory(Environment.getDataDirectory());
    }
    
    // Get external (SDCARD) free space
    public long getFreeExternalMemory()
    {
        return getFreeMemory(Environment.getExternalStorageDirectory());
    }
    
    // Get Android OS (system partition) free space
    public long getFreeSystemMemory()
    {
        return getFreeMemory(Environment.getRootDirectory());
    }
    
    // Get free space for provided path
    // Note that this will throw IllegalArgumentException for invalid paths
    public long getFreeMemory(File path)
    {
        StatFs stats = new StatFs(path.getAbsolutePath());
        return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
    }
    

    Typecast your integer values to long before doing multiplication. Multiplication between two big integers could overflow and give a negative result.

    public long sd_card_free(){
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        int availBlocks = stat.getAvailableBlocks();
        int blockSize = stat.getBlockSize();
        long free_memory = (long)availBlocks * (long)blockSize;
    
        return free_memory;
    }
    

    With a little google you might had found the StatFs-class which is:

    […] a Wrapper for Unix statfs().

    Examples are provided

    • here
    • and also here.
        File pathOS = Environment.getRootDirectory();//Os Storage
        StatFs statOS = new StatFs(pathOS.getPath());
    
        File pathInternal = Environment.getDataDirectory();// Internal Storage
      StatFs statInternal = new StatFs(pathInternal.getPath());
    
        File pathSdcard = Environment.getExternalStorageDirectory();//External(SD CARD) Storage
        StatFs statSdcard = new StatFs(pathSdcard.getPath());
    
        if((android.os.Build.VERSION.SDK_INT < 18)) {
            // Get Android OS (system partition) free space API 18 & Below
            int totalBlocksOS = statOS.getBlockCount();
            int blockSizeOS = statOS.getBlockSize();
            int availBlocksOS = statOS.getAvailableBlocks();
            long total_OS_memory = (long) totalBlocksOS * (long) blockSizeOS;
            long free_OS_memory = (long) availBlocksOS * (long) blockSizeOS;
            long Used_OS_memory = total_OS_memory - free_OS_memory;
            TotalOsMemory       =   total_OS_memory ;
            FreeOsMemory        =   free_OS_memory;
            UsedOsMemory        =   Used_OS_memory;
    
            // Get internal (data partition) free space API 18 & Below
            int totalBlocksInternal = statInternal.getBlockCount();
            int blockSizeInternal = statOS.getBlockSize();
            int availBlocksInternal = statInternal.getAvailableBlocks();
            long total_Internal_memory = (long) totalBlocksInternal * (long) blockSizeInternal;
            long free_Internal_memory = (long) availBlocksInternal * (long) blockSizeInternal;
            long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
            TotalInternalMemory =   total_Internal_memory;
            FreeInternalMemory  =   free_Internal_memory ;
            UsedInternalMemory  =   Used_Intenal_memory ;
    
            // Get external (SDCARD) free space for API 18 & Below
            int totalBlocksSdcard = statSdcard.getBlockCount();
            int blockSizeSdcard = statOS.getBlockSize();
            int availBlocksSdcard = statSdcard.getAvailableBlocks();
            long total_Sdcard_memory = (long) totalBlocksSdcard * (long) blockSizeSdcard;
            long free_Sdcard_memory = (long) availBlocksSdcard * (long) blockSizeSdcard;
            long Used_Sdcard_memory = total_Sdcard_memory - free_Sdcard_memory;
            TotalSdcardMemory   =   total_Sdcard_memory;
            FreeSdcardMemory    =   free_Sdcard_memory;
            UsedSdcardMemory    =   Used_Sdcard_memory;
        }
        else {
            // Get Android OS (system partition) free space for API 18 & Above
            long   total_OS_memory          = (statOS.       getBlockCountLong()      * statOS.getBlockSizeLong());
            long   free_OS_memory           = (statOS.       getAvailableBlocksLong() * statOS.getBlockSizeLong());
            long Used_OS_memory = total_OS_memory - free_OS_memory;
            TotalOsMemory       =   total_OS_memory ;
            FreeOsMemory        =   free_OS_memory;
            UsedOsMemory        =   Used_OS_memory;
    
            // Get internal (data partition) free space for API 18 & Above
            long   total_Internal_memory    = (statInternal. getBlockCountLong()      * statInternal.getBlockSizeLong());
            long   free_Internal_memory     = (statInternal. getAvailableBlocksLong() * statInternal.getBlockSizeLong());
            long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
            TotalInternalMemory =   total_Internal_memory;
            FreeInternalMemory  =   free_Internal_memory ;
            UsedInternalMemory  =   Used_Intenal_memory ;
    
            // Get external (SDCARD) free space for API 18 & Above
            long   total_Sdcard_memory      = (statSdcard.   getBlockCountLong()      * statSdcard.getBlockSizeLong());
            long   free_Sdcard_memory       = (statSdcard.   getAvailableBlocksLong() * statSdcard.getBlockSizeLong());
            long Used_Sdcard_memory = tota*emphasized text*l_Sdcard_memory - free_Sdcard_memory;
            TotalSdcardMemory   =   total_Sdcard_memory;
            FreeSdcardMemory    =   free_Sdcard_memory;
            UsedSdcardMemory    =   Used_Sdcard_memory;
        }
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.