Example usage for android.os StatFs getBlockSize

List of usage examples for android.os StatFs getBlockSize

Introduction

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

Prototype

@Deprecated
public int getBlockSize() 

Source Link

Usage

From source file:com.sqkj.engine.image.ImageCache.java

/**
 * Check how much usable space is available at a given path.
 *
 * @param path The path to check//from   w  ww .  j  a v a2  s .  c  o m
 * @return The space available in bytes
 */
public static long getUsableSpace(File path) {
    if (SdkUtils.hasGingerbread()) {
        return path.getUsableSpace();
    }
    final StatFs stats = new StatFs(path.getPath());
    return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
}

From source file:com.lxh.util.image.OtherUtils.java

/**
 * ??//from  w  w w . java2  s.c om
 * @param dir {@link java.io.File}
 * @return ????byte-1
 */
public static long getAvailableSpace(File dir) {
    try {
        final StatFs stats = new StatFs(dir.getPath());
        return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
    } catch (Throwable e) {
        return -1;
    }
}

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./* w w  w  .  ja v  a2s. 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

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static Boolean hasExternalStorageFreeMemory() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double sdAvailSize;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        sdAvailSize = (double) stat.getAvailableBlocksLong() * (double) stat.getBlockSizeLong();
    } else {//from   w  w  w .  j ava  2 s  . com
        sdAvailSize = (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
    }

    return sdAvailSize >= 10 * SIZE_MB;
}

From source file:Main.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static long getExternalStorageFreeMemory() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double sdAvailSize;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        sdAvailSize = (double) stat.getAvailableBlocksLong() * (double) stat.getBlockSizeLong();
    } else {//w ww.j av a2  s. c o  m
        sdAvailSize = (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
    }

    return Math.round(sdAvailSize / SIZE_MB);
}

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  w w  w .java 2 s.  co m
 *
 * @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:org.proninyaroslav.libretorrent.core.utils.FileIOUtils.java

public static long getFreeSpace(String path) {
    long availableBytes = -1L;

    try {//from w w  w. j  a  v a  2s.com
        File file = new File(path);
        availableBytes = file.getUsableSpace();
    } catch (Exception e) {

        // this provides invalid space on some devices
        try {
            StatFs stat = new StatFs(path);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                availableBytes = stat.getAvailableBytes();
            } else {
                availableBytes = stat.getAvailableBlocks() * stat.getBlockSize();
            }
        } catch (Exception ee) {
            /* Ignore */
        }
    }

    return availableBytes;
}

From source file:Main.java

/**
 * Get the number of tiles that can be stored on the file system.
 *
 * @param directory where the cache will reside
 * @param fileSize  average size of tile to be cached
 * @return number of tiles that can be stored without running out of space
 */// w  ww.  j ava2  s  .  c  o m
@SuppressWarnings("deprecation")
@TargetApi(18)
public static long getAvailableCacheSlots(String directory, int fileSize) {
    StatFs statfs = new StatFs(directory);
    if (android.os.Build.VERSION.SDK_INT >= 18) {
        return statfs.getAvailableBytes() / fileSize;
    }
    // problem is overflow with devices with large storage, so order is important here
    // additionally avoid division by zero in devices with a large block size
    int blocksPerFile = Math.max(fileSize / statfs.getBlockSize(), 1);
    return statfs.getAvailableBlocks() / blocksPerFile;
}

From source file:eu.geopaparazzi.library.util.Utilities.java

/**
 * Get the size in megabytes of the filesystem at 'file'.
 *
 * @param file the filesystem's path.//from   w  ww.  j  a  v a2  s .c om
 * @return the size in mb.
 */
public static float getFilesystemMegabytes(File file) {
    StatFs stat = new StatFs(file.getPath());
    long bytes = (long) stat.getBlockSize() * (long) stat.getBlockCount();
    return bytes / (1024.f * 1024.f);
}

From source file:id.wibisana.priadimulia.imagecaching.cache.ImageCache.java

@SuppressWarnings("deprecation")
@TargetApi(9)//from  w ww  .j  a v a 2s  . c o  m
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();
}