Example usage for android.os StatFs getAvailableBlocksLong

List of usage examples for android.os StatFs getAvailableBlocksLong

Introduction

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

Prototype

public long getAvailableBlocksLong() 

Source Link

Document

The number of blocks that are free on the file system and available to applications.

Usage

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

public static long FreeMemory() {
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
    long Free = (statFs.getAvailableBlocksLong() * statFs.getBlockSize()) / 1048576;
    return Free;/*from w ww  .  j a  va  2  s  .  c  o  m*/
}

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

@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;
    }//from  www .  j ava 2s .  c om
    return -1;
}

From source file:Main.java

public static long getAvailableMemory(StatFs stat) {
    try {/*from w w  w .j  av a 2 s.co m*/
        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

@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 {/*from  w w  w  .j  av a2 s  . com*/
        return -1;
    }
    return freeSpace;
}

From source file:Main.java

/**
 * Helper method to calculate the proper size limit of a cache instance.
 *///from w  ww  .  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

@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  www. j  ava  2s .c o  m
        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 {//from   w  w  w  .ja  v  a 2 s.  c  om
        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./*  w  w  w. j av a2s  .  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;
}