Example usage for android.os StatFs StatFs

List of usage examples for android.os StatFs StatFs

Introduction

In this page you can find the example usage for android.os StatFs StatFs.

Prototype

public StatFs(String path) 

Source Link

Document

Construct a new StatFs for looking at the stats of the filesystem at path .

Usage

From source file:Main.java

/**
 * Check how much usable space is available at a given path.
 * //ww w.  j av  a  2 s . co  m
 * @param path
 *            The path to check
 * @return The space available in bytes
 */
public static long getUsableSpace(File path) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return path.getUsableSpace();
    }
    final StatFs stats = new StatFs(path.getPath());
    return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
}

From source file:Main.java

/**
 * get the space is left over on phone self
 *///w  ww.ja v a 2  s  . c  o m
public static long getRealSizeOnPhone() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    long realSize = blockSize * availableBlocks;
    return realSize;
}

From source file:Main.java

/**
 * Calculates the total memory of the device. This is based on an inspection of the filesystem, which in android
 * devices is stored in RAM.//from w  w  w.j  a v a 2  s  .  c  o m
 *
 * @return Total number of bytes.
 */
public static long getTotalInternalMemorySize() {
    final File path = Environment.getDataDirectory();
    final StatFs stat = new StatFs(path.getPath());
    final long blockSize;
    final long totalBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = stat.getBlockSizeLong();
        totalBlocks = stat.getBlockCountLong();
    } else {
        //noinspection deprecation
        blockSize = stat.getBlockSize();
        //noinspection deprecation
        totalBlocks = stat.getBlockCount();
    }
    return totalBlocks * blockSize;
}

From source file:Main.java

/**
 * Calculates the free memory of the device. This is based on an inspection of the filesystem, which in android
 * devices is stored in RAM./*w ww  . jav a  2  s.com*/
 *
 * @return Number of bytes available.
 */
public static long getAvailableInternalMemorySize() {
    final File path = Environment.getDataDirectory();
    final StatFs stat = new StatFs(path.getPath());
    final long blockSize;
    final long availableBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = stat.getBlockSizeLong();
        availableBlocks = stat.getAvailableBlocksLong();
    } else {
        //noinspection deprecation
        blockSize = stat.getBlockSize();
        //noinspection deprecation
        availableBlocks = stat.getAvailableBlocks();
    }
    return availableBlocks * blockSize;
}

From source file:Main.java

public static boolean hasEnoughSpace(float needSize) {
    if (isSDCardExist()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());

        long blockSize;
        long availCount;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = sf.getBlockSizeLong();
            availCount = sf.getAvailableBlocksLong();
        } else {// ww  w  . ja  v  a  2 s.  c om
            blockSize = sf.getBlockSize();
            availCount = sf.getAvailableBlocks();
        }

        long restSize = availCount * blockSize;
        if (restSize > needSize) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Helper method to calculate the proper size limit of a cache instance.
 *//*www .  ja  va 2 s .c  om*/
public static long getCacheSizeInBytes(File dir, float cacheSizePercent, long maxSizeInBytes) {
    if (dir == null || (!dir.exists() && !dir.mkdir())) {
        return 0;
    }
    try {
        StatFs stat = new StatFs(dir.getPath());
        long totalBytes = stat.getBlockCountLong() * stat.getBlockSizeLong();
        long freeBytes = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
        long desiredBytes = Math.min((long) (totalBytes * cacheSizePercent), maxSizeInBytes);
        // If free space is less than desired, use half of the free disk space instead.
        desiredBytes = (desiredBytes > freeBytes) ? freeBytes / 2 : desiredBytes;
        return desiredBytes;
    } catch (IllegalArgumentException e) {
        return 0;
    }
}

From source file:Main.java

/**
 * **********************************************************************************************
 * Returns size in MegaBytes./*  w w  w .j  a  v a  2s  .c om*/
 *
 * @author Dan
 * <p/>
 * 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 static long TotalMemory() {
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
    long Total = (statFs.getBlockCountLong() * statFs.getBlockSize()) / 1048576;
    return Total;
}

From source file:Main.java

public static long getUsableSpace(File path) {
    final StatFs stats = new StatFs(path.getPath());
    return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
}

From source file:Main.java

/**
 * Returns the amount of storage currently available in the cache.
 *
 * @param dir The cache directory path name.
 * @return The amount of storage available in bytes.
 *///from   ww  w.j  av a  2s . co m
public static long calculateDiskCacheSize(File dir) {
    long size = MIN_DISK_CACHE_SIZE;

    try {
        StatFs statFs = new StatFs(dir.getAbsolutePath());
        long available = statFs.getBlockCountLong() * statFs.getBlockSizeLong();
        // Target 2% of the total space.
        size = available * MAX_DISK_CACHE_AS_PERCENT / 100;
    } catch (IllegalArgumentException ignored) {
    }

    // Bound inside min/max size for disk cache.
    return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
}

From source file:Main.java

public static int freeSpaceOnSD() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double sdFreeMB = ((double) stat.getAvailableBlocks() * (double) stat.getBlockSize()) / 1024 * 1024;
    return (int) sdFreeMB;
}