Java Code Examples for android.os.StatFs#getFreeBlocks()

The following examples show how to use android.os.StatFs#getFreeBlocks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageActivity.java    From AndroidDemo with MIT License 6 votes vote down vote up
/**
 * 除去系统后的内存大小
 */
private void checkExceptSystemCapacity() {
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());

    //存储块
    long blockCount = statFs.getBlockCount();
    //块大小
    long blockSize = statFs.getBlockSize();
    //可用块数量
    long availableCount = statFs.getAvailableBlocks();
    //剩余块数量,注:这个包含保留块(including reserved blocks)即应用无法使用的空间
    long freeBlocks = statFs.getFreeBlocks();

    long totalSize = statFs.getTotalBytes();
    long availableSize = statFs.getAvailableBytes();

    append("total = " + getUnit(totalSize));
    append("availableSize = " + getUnit(availableSize));
    append("=========");
    append("total = " + getUnit(blockSize * blockCount));
    append("available = " + getUnit(blockSize * availableCount));
    append("free = " + getUnit(blockSize * freeBlocks));
}
 
Example 2
Source File: StorageQueryUtil.java    From AndroidDemo with MIT License 6 votes vote down vote up
public static void queryWithStatFs() {
        StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());

        //存储块
        long blockCount = statFs.getBlockCount();
        //块大小
        long blockSize = statFs.getBlockSize();
        //可用块数量
        long availableCount = statFs.getAvailableBlocks();
        //剩余块数量,注:这个包含保留块(including reserved blocks)即应用无法使用的空间
        long freeBlocks = statFs.getFreeBlocks();

        //level 18
//        long totalSize = statFs.getTotalBytes();
//        long availableSize = statFs.getAvailableBytes();

        Log.d(TAG, "=========");
        Log.d(TAG, "total = " + getUnit(blockSize * blockCount, 1024));
        Log.d(TAG, "available = " + getUnit(blockSize * availableCount, 1024));
        Log.d(TAG, "free = " + getUnit(blockSize * freeBlocks, 1024));
    }
 
Example 3
Source File: StatFsHelper.java    From fresco with MIT License 6 votes vote down vote up
/**
 * Gets the information about the free storage space, including reserved blocks, either internal
 * or external depends on the given input
 *
 * @param storageType Internal or external storage type
 * @return available space in bytes, -1 if no information is available
 */
@SuppressLint("DeprecatedMethod")
public long getFreeStorageSpace(StorageType storageType) {
  ensureInitialized();

  maybeUpdateStats();

  StatFs statFS = storageType == StorageType.INTERNAL ? mInternalStatFs : mExternalStatFs;
  if (statFS != null) {
    long blockSize, availableBlocks;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
      blockSize = statFS.getBlockSizeLong();
      availableBlocks = statFS.getFreeBlocksLong();
    } else {
      blockSize = statFS.getBlockSize();
      availableBlocks = statFS.getFreeBlocks();
    }
    return blockSize * availableBlocks;
  }
  return -1;
}
 
Example 4
Source File: FileUtil.java    From android-file-chooser with Apache License 2.0 6 votes vote down vote up
public static long readSDCard(Context context, Boolean isRemovable, Boolean freeOrTotal) {
    DecimalFormat df = new DecimalFormat("0.00");
    getStoragePath(context, isRemovable);
    StatFs sf = new StatFs(getStoragePath(context, isRemovable));
    long blockSize;
    long blockCount;
    long availCount;
    if (Build.VERSION.SDK_INT > 18) {
        blockSize = sf.getBlockSizeLong(); //文件存储时每一个存储块的大小为4KB
        blockCount = sf.getBlockCountLong();//存储区域的存储块的总个数
        availCount = sf.getFreeBlocksLong();//存储区域中可用的存储块的个数(剩余的存储大小)
    } else {
        blockSize = sf.getBlockSize();
        blockCount = sf.getBlockCount();
        availCount = sf.getFreeBlocks();
    }
    //Log.d("sss", "总的存储空间大小:" + blockSize * blockCount / 1073741824 + "GB" + ",剩余空间:"
    //    + availCount * blockSize / 1073741824 + "GB"
    //    + "--存储块的总个数--" + blockCount + "--一个存储块的大小--" + blockSize / 1024 + "KB");
    //return df.format((freeOrTotal ? availCount : blockCount) * blockSize / 1073741824.0);
    return (freeOrTotal ? availCount : blockCount) * blockSize;
    //return "-1";
}
 
Example 5
Source File: SDKUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
public static Long getFreeSpaceSize(StatFs statFs) {
    long freeSpaceSize = 0;
    try {
        if (VERSION.SDK_INT >= 18) {
            freeSpaceSize = statFs.getFreeBlocksLong() * statFs.getBlockSizeLong();
        } else {
            freeSpaceSize = ((long) statFs.getFreeBlocks()) * ((long) statFs.getBlockSize());
        }
    } catch (Throwable e) {
        LOG.w(TAG, "getScreenHeightWidth failed(Throwable): " + e.getMessage());
    }
    return Long.valueOf(freeSpaceSize);
}
 
Example 6
Source File: RLAPICompat.java    From Roid-Library with Apache License 2.0 5 votes vote down vote up
/**
 * @return
 */
public static long getFreeBlocks() {
    long size = 0;
    if (Environment.getExternalStorageDirectory() != null) {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        size = VERSION >= 18 ? SDK18.getFreeBlocks() : (long) stat.getFreeBlocks();
    }
    return size;
}