Example usage for android.os StatFs getBlockSizeLong

List of usage examples for android.os StatFs getBlockSizeLong

Introduction

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

Prototype

public long getBlockSizeLong() 

Source Link

Document

The size, in bytes, of a block on the file system.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getSDFreeSize() {
    File path = Environment.getExternalStorageDirectory();
    if (path != null && path.exists() && path.isDirectory()) {
        StatFs sf = new StatFs(path.getPath());
        long blockSize = sf.getBlockSizeLong();
        long freeBlocks = sf.getAvailableBlocksLong();
        return (freeBlocks * blockSize) / 1024 / 1024;
    }/* w w w . ja v a2s .co m*/
    return -1;
}

From source file:Main.java

@SuppressLint("NewApi")
public static long getFreeDiskSpace() {
    String status = Environment.getExternalStorageState();
    long freeSpace = 0;
    if (status.equals(Environment.MEDIA_MOUNTED)) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long availableBlock = stat.getAvailableBlocksLong();
        freeSpace = availableBlock * blockSize / 1024;
    } else {/* w  w  w . ja v a  2s  . co m*/
        return -1;
    }
    return freeSpace;
}

From source file:Main.java

/**
 * Helper method to calculate the proper size limit of a cache instance.
 *///from  w  w  w.ja  v a 2  s. c  o m
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

public static long freeSpaceOnSD() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());

    return stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
}

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 w  ww.jav  a 2s  .  c o 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 freeSpaceOnSDMB() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double sdFreeMB = ((double) stat.getAvailableBlocksLong() * (double) stat.getBlockSizeLong()) / 1024 * 1024;

    return (int) sdFreeMB;
}

From source file:Main.java

public static long getAvailableMemory(StatFs stat) {
    try {//w  w w.j ava 2 s.  c om
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
        }
    } catch (Exception e) {
        //for some reason, it appears some devices even in jelly bean don't have this method.
    }

    return 0;
}

From source file:Main.java

/**
 * Gets the free disk space at the specified location.
 *
 * @param dir/*  www . j av a 2  s.c  om*/
 *         the directory linking to the filesystem to query
 * @return the free disk space at {@code dir} in bytes
 */
private static long getFreeDiskSpace(File dir) {
    StatFs statFs = new StatFs(dir.getAbsolutePath());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return statFs.getBlockCountLong() * statFs.getBlockSizeLong();
    } else {
        //noinspection deprecation
        return statFs.getBlockCount() * statFs.getBlockSize();
    }
}

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  a2  s.  co 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.//from   www  . j a v a 2s  .c  om
 *
 * @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;
}