How to find the amount of free storage (disk space) left on Android? [duplicate]
Asked Answered
C

10

43

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,

Chopper answered 18/8, 2011 at 22:41 Comment(1)
closed as duplicate - newer post has answers up to Android RSephira
A
16

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

Auklet answered 18/8, 2011 at 22:50 Comment(2)
For API level >= 18 - StatFs.getAvailableBytes () Returns the number of bytes that are free on the file system and available to applications.Profant
getAvailableBlocks was deprecated for api level 18, the new methods is getAvailableBlocksLongBothwell
D
54

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 "???";
    }
Disinfect answered 11/1, 2012 at 20:46 Comment(2)
getAvailableBlocks() and getBlockSize() have since been deprecated and replaced with getAvailableBlocksLong() and statFs.getBlockSizeLong(), respectively.Fibrinolysis
Environment.getRootDirectory() means you are querying the system / OS partition for total, free and busy memory. To get stats on internal memory available for apps, you should be querying the data partition instead. See https://mcmap.net/q/378497/-how-to-find-the-amount-of-free-storage-disk-space-left-on-android-duplicate.Saltish
A
16

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

Auklet answered 18/8, 2011 at 22:50 Comment(2)
For API level >= 18 - StatFs.getAvailableBytes () Returns the number of bytes that are free on the file system and available to applications.Profant
getAvailableBlocks was deprecated for api level 18, the new methods is getAvailableBlocksLongBothwell
S
15

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();
}
Saltish answered 19/2, 2016 at 18:45 Comment(1)
you have summarized very well, but getAvailableBlocks() and getBlockSize() both methods are deprecated. Please update your anwser.Symbolic
S
8

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.getAvailableBlocksLong();
    int blockSize = stat.getBlockSizeLong();
    long free_memory = (long)availBlocks * (long)blockSize;

    return free_memory;
}
Stuckey answered 26/4, 2013 at 2:37 Comment(2)
+1 Presumably this is why getAvailableBlocks() and getBlockSize() have been deprecated and replaced by getAvailableBlocksLong() and statFs.getBlockSizeLong().Fibrinolysis
Better use long variables for availBlocks and blockSize from the getgo.Undenominational
W
8

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.

Wack answered 16/7, 2013 at 10:22 Comment(0)
R
6

The StatFs-class is:

[...] a Wrapper for Unix statfs().

Examples are here and here:

import java.io.File;

import android.os.Environment;
import android.os.StatFs;

public class MemoryStatus {

    static final int ERROR = -1;
    
    static public boolean externalMemoryAvailable() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
    
    static public long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }
    
    static public long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    }
    
    static public long getAvailableExternalMemorySize() {
        if(externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return ERROR;
        }
    }
    
    static public long getTotalExternalMemorySize() {
        if(externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return totalBlocks * blockSize;
        } else {
            return ERROR;
        }
    }
    
    static public String formatSize(long size) {
        String suffix = null;
    
        if (size >= 1024) {
            suffix = "KiB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MiB";
                size /= 1024;
            }
        }
    
        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
    
        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }
    
        if (suffix != null)
            resultBuffer.append(suffix);
        return resultBuffer.toString();
    }
}

Source for code above (Wayback Machine)

Rena answered 18/8, 2011 at 22:49 Comment(2)
Second link is brokenDonnie
@youravgjoe got it back with the Wayback Machine.Rena
C
3
    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;
    }
}
Claudclauddetta answered 22/10, 2016 at 21:13 Comment(1)
private Long TotalOsMemory,FreeOsMemory,UsedOsMemory,TotalInternalMemory,FreeInternalMemory,UsedInternalMemory,TotalSdcardMemory,FreeSdcardMemory,UsedSdcardMemory;Claudclauddetta
B
3

Since blocksize and getAvailableBlocks

are deprecated

this code can be use

note based above answer by user802467

public long sd_card_free(){
    File path = Environment.getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    long availBlocks = stat.getAvailableBlocksLong();
    long blockSize = stat.getBlockSizeLong();
    long free_memory = availBlocks * blockSize;

    return free_memory;
}

we can use getAvailableBlocksLong and getBlockSizeLong

Beyond answered 21/9, 2017 at 10:29 Comment(0)
S
3
/**
 * Returns the amount of free memory.
 * @return {@code long} - Free space.
 */
public long getFreeInternalMemory() {
    return getFreeMemory(Environment.getDataDirectory());
}

/**
 * Returns the free amount in SDCARD.
 * @return {@code long} - Free space.
 */
public long getFreeExternalMemory() {
    return getFreeMemory(Environment.getExternalStorageDirectory());
}

/**
 * Returns the free amount in OS.
 * @return {@code long} - Free space.
 */
public long getFreeSystemMemory() {
    return getFreeMemory(Environment.getRootDirectory());
}

/**
 * Returns the free amount in mounted path
 * @param path {@link File} - Mounted path.
 * @return {@code long} - Free space.
 */
public long getFreeMemory(File path) {
    if ((null != path) && (path.exists()) && (path.isDirectory())) {
        StatFs stats = new StatFs(path.getAbsolutePath());
        return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
    }
    return -1;
}

/**
 * Convert bytes to human format.
 * @param totalBytes {@code long} - Total of bytes.
 * @return {@link String} - Converted size.
 */
public String bytesToHuman(long totalBytes) {
    String[] simbols = new String[] {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
    long scale = 1L;
    for (String simbol : simbols) {
        if (totalBytes < (scale * 1024L)) {
            return String.format("%s %s", new DecimalFormat("#.##").format((double)totalBytes / scale), simbol);
        }
        scale *= 1024L;
    }
    return "-1 B";
}
Scrapple answered 30/11, 2018 at 20:6 Comment(0)
F
1

Memory Locations:

File[] roots = context.getExternalFilesDirs(null);
String path = roots[0].getAbsolutePath(); // PhoneMemory
String path = roots[1].getAbsolutePath(); // SCCard (if available)
String path = roots[2].getAbsolutePath(); // USB (if available)

usage

long totalMemory = StatUtils.totalMemory(path);
long freeMemory = StatUtils.freeMemory(path);

final String totalSpace = StatUtils.humanize(totalMemory, true);
final String usableSpace = StatUtils.humanize(freeMemory, true);

You can use this

public final class StatUtils {

    public static long totalMemory(String path) {
        StatFs statFs = new StatFs(path);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //noinspection deprecation
            return (statFs.getBlockCount() * statFs.getBlockSize());
        } else {
            return (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
        }
    }

    public static long freeMemory(String path) {
        StatFs statFs = new StatFs(path);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //noinspection deprecation
            return (statFs.getAvailableBlocks() * statFs.getBlockSize());
        } else {
            return (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
        }
    }

    public static long usedMemory(String path) {
        long total = totalMemory(path);
        long free = freeMemory(path);
        return total - free;
    }

    public static String humanize(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
        return String.format(Locale.ENGLISH, "%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }
}
Florina answered 6/11, 2017 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.