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:com.squareup.picasso3.Utils.java

static long calculateDiskCacheSize(File dir) {
    long size = MIN_DISK_CACHE_SIZE;

    try {//from  w w  w. j a v a  2 s. c  o  m
        StatFs statFs = new StatFs(dir.getAbsolutePath());
        //noinspection deprecation
        long blockCount = SDK_INT < JELLY_BEAN_MR2 ? (long) statFs.getBlockCount() : statFs.getBlockCountLong();
        //noinspection deprecation
        long blockSize = SDK_INT < JELLY_BEAN_MR2 ? (long) statFs.getBlockSize() : statFs.getBlockSizeLong();
        long available = blockCount * blockSize;
        // Target 2% of the total space.
        size = available / 50;
    } 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:com.cyberocw.habittodosecretary.file.StorageHelper.java

/**
 * Returns a directory size in bytes//  ww w.  j  a  v  a 2s  .  com
 */
@SuppressWarnings("deprecation")
public static long getSize(File directory) {
    StatFs statFs = new StatFs(directory.getAbsolutePath());
    long blockSize = 0;
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = statFs.getBlockSizeLong();
        } else {
            blockSize = statFs.getBlockSize();
        }
        // Can't understand why on some devices this fails
    } catch (NoSuchMethodError e) {
        Log.e(Const.ERROR_TAG, "Mysterious error", e);
    }
    return getSize(directory, blockSize);
}

From source file:com.dycody.android.idealnote.utils.StorageHelper.java

/**
 * Returns a directory size in bytes/*  w  ww .  j  av  a  2 s . c  om*/
 */
@SuppressWarnings("deprecation")
public static long getSize(File directory) {
    StatFs statFs = new StatFs(directory.getAbsolutePath());
    long blockSize = 0;
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = statFs.getBlockSizeLong();
        } else {
            blockSize = statFs.getBlockSize();
        }
        // Can't understand why on some devices this fails
    } catch (NoSuchMethodError e) {
        Log.e(Constants.TAG, "Mysterious error", e);
    }
    return getSize(directory, blockSize);
}

From source file:it.feio.android.omninotes.utils.StorageManager.java

/**
 * Returns a directory size in bytes//from   w  ww .java2s . c  o m
 * @param directory
 * @return
 */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static long getSize(File directory) {
    StatFs statFs = new StatFs(directory.getAbsolutePath());
    long blockSize = 0;
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = statFs.getBlockSizeLong();
        } else {
            blockSize = statFs.getBlockSize();
        }
        // Can't understand why on some devices this fails
    } catch (NoSuchMethodError e) {
    }

    return getSize(directory, blockSize);
}

From source file:count.ly.messaging.CrashDetails.java

/**
 * Returns the current device disk space.
 *//*from  w w w .j  av  a 2 s  . c o m*/
static String getDiskTotal() {
    if (android.os.Build.VERSION.SDK_INT < 18) {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = (statFs.getBlockCount() * statFs.getBlockSize());
        return Long.toString(total / 1048576L);
    } else {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
        return Long.toString(total / 1048576L);
    }
}

From source file:prince.app.sphotos.util.ImageCache.java

/**
 * Check how much usable space is available at a given path.
 *
 * @param path The path to check/*  w  ww . j av a  2  s  . c o m*/
 * @return The space available in bytes
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getUsableSpace(File path) {
    if (Global.hasGingerbread()) {
        return path.getUsableSpace();
    }
    final StatFs stats = new StatFs(path.getPath());
    return (long) stats.getBlockSizeLong() * (long) stats.getAvailableBlocksLong();
}

From source file:ly.count.android.sdk.CrashDetails.java

/**
 * Returns the current device disk space.
 *///from w w  w.j  a v  a 2 s .co m
@TargetApi(18)
static String getDiskTotal() {
    if (android.os.Build.VERSION.SDK_INT < 18) {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        return Long.toString(total / 1048576L);
    } else {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
        return Long.toString(total / 1048576L);
    }
}

From source file:count.ly.messaging.CrashDetails.java

/**
 * Returns the current device disk space.
 *//* w  ww.j ava 2  s .  c om*/
static String getDiskCurrent() {
    if (android.os.Build.VERSION.SDK_INT < 18) {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = (statFs.getBlockCount() * statFs.getBlockSize());
        long free = (statFs.getAvailableBlocks() * statFs.getBlockSize());
        return Long.toString((total - free) / 1048576L);
    } else {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
        long free = (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
        return Long.toString((total - free) / 1048576L);
    }
}

From source file:ly.count.android.sdk.CrashDetails.java

/**
 * Returns the current device disk space.
 *//*from  w  ww.  ja  v a2 s  . co m*/
@TargetApi(18)
static String getDiskCurrent() {
    if (android.os.Build.VERSION.SDK_INT < 18) {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize());
        long free = ((long) statFs.getAvailableBlocks() * (long) statFs.getBlockSize());
        return Long.toString((total - free) / 1048576L);
    } else {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long total = (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
        long free = (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
        return Long.toString((total - free) / 1048576L);
    }
}

From source file:com.just.agentweb.AgentWebUtils.java

public static long getAvailableStorage() {
    try {/*from w  w  w.ja  va 2s  .c om*/
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().toString());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            return stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
        } else {
            return (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
        }
    } catch (RuntimeException ex) {
        return 0;
    }
}