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

The following examples show how to use android.os.StatFs#getAvailableBlocks() . 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: 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 2
Source File: DirectoryFragment.java    From SSForms with GNU General Public License v3.0 5 votes vote down vote up
private String getRootSubtitle(String path) {
    StatFs stat = new StatFs(path);
    long total = (long) stat.getBlockCount() * (long) stat.getBlockSize();
    long free = (long) stat.getAvailableBlocks()
            * (long) stat.getBlockSize();
    if (total == 0) {
        return "";
    }
    return "Free " + formatFileSize(free) + " of " + formatFileSize(total);
}
 
Example 3
Source File: Utilities.java    From Android-Remote with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the free space on the internal storage device
 *
 * @return The free space in byte
 */
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static double getFreeSpaceInternal() {
    StatFs stat = new StatFs(App.getApp().getFilesDir().getPath());
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
    } else {
        return (double) stat.getAvailableBlocksLong() * (double) stat.getBlockSizeLong();
    }
}
 
Example 4
Source File: SDCardUtil.java    From MVPAndroidBootstrap with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the available space in megabytes.
 * @return
 */
public static int getAvailableSpaceInMegaBytes() {
    int availableSpace = 0;

    try {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
        availableSpace = stat.getAvailableBlocks() * stat.getBlockSize() / 1048576;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return availableSpace;
}
 
Example 5
Source File: ImageCache.java    From RoMote with Apache License 2.0 5 votes vote down vote up
/**
 * Check how much usable space is available at a given path.
 *
 * @param path The path to check
 * @return The space available in bytes
 */
@TargetApi(VERSION_CODES.GINGERBREAD)
public static long getUsableSpace(File path) {
    if (Utils.hasGingerbread()) {
        return path.getUsableSpace();
    }
    final StatFs stats = new StatFs(path.getPath());
    return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
}
 
Example 6
Source File: DirectoryManager.java    From reader with MIT License 5 votes vote down vote up
/**
 * Given a path return the number of free KB
 * 
 * @param path to the file system
 * @return free space in KB
 */
private static long freeSpaceCalculation(String path) {
    StatFs stat = new StatFs(path);
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return availableBlocks * blockSize / 1024;
}
 
Example 7
Source File: api_o.java    From styT with Apache License 2.0 5 votes vote down vote up
private String getRomAvailableSize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return Formatter.formatFileSize(this, blockSize * availableBlocks);
}
 
Example 8
Source File: FileUtils.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 获得sd卡剩余容量,即可用大小
 *
 * @return
 */
public static String getSDAvailableSize(Context ontext) {
    File path = Environment.getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return Formatter.formatFileSize(ontext, blockSize * availableBlocks);
}
 
Example 9
Source File: Utility.java    From Kernel-Tuner with GNU General Public License v3.0 5 votes vote down vote up
public static long getAvailableSpaceInBytesOnInternalStorage()
{
    long availableSpace;
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    availableSpace = (long) stat.getAvailableBlocks()
            * (long) stat.getBlockSize();

    return availableSpace;
}
 
Example 10
Source File: AndroidUtils.java    From Aria with Apache License 2.0 5 votes vote down vote up
/**
 * Check how much usable space is available at a given path.
 *
 * @param path The path to check
 * @return The space available in bytes
 */
@TargetApi(Build.VERSION_CODES.GINGERBREAD) public static long getUsableSpace(File path) {
  if (AndroidVersionUtil.hasGingerbread()) {
    return path.getUsableSpace();
  }
  final StatFs stats = new StatFs(path.getPath());
  return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
}
 
Example 11
Source File: ExternalStorage.java    From KrGallery with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获取目录剩余空间
 * @param directoryPath
 * @return
 */
private long getResidualSpace(String directoryPath) {
    try {
        StatFs sf = new StatFs(directoryPath);
        long blockSize = sf.getBlockSize();
        long availCount = sf.getAvailableBlocks();
        long availCountByte = availCount * blockSize;
        return availCountByte;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
 
Example 12
Source File: RxFileTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * 获取某个目录可用大小.
 */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static long getDirSize(String path) {
    StatFs stat = new StatFs(path);
    long blockSize, availableBlocks;
    if (Build.VERSION.SDK_INT >= 18) {
        blockSize = stat.getBlockSizeLong();
        availableBlocks = stat.getAvailableBlocksLong();
    } else {
        blockSize = stat.getBlockSize();
        availableBlocks = stat.getAvailableBlocks();
    }
    return availableBlocks * blockSize;
}
 
Example 13
Source File: DeviceUtils.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
/**
 * @return Number of bytes available on external storage
 */
public static long getExternalAvailableSpaceInBytes() {
    long availableSpace = -1L;
    try {
        StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
                .getPath());
        availableSpace = (long) stat.getAvailableBlocks()
                * (long) stat.getBlockSize();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return availableSpace;
}
 
Example 14
Source File: DirectoryManager.java    From reader with MIT License 5 votes vote down vote up
/**
 * Given a path return the number of free KB
 * 
 * @param path to the file system
 * @return free space in KB
 */
private static long freeSpaceCalculation(String path) {
    StatFs stat = new StatFs(path);
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    return availableBlocks * blockSize / 1024;
}
 
Example 15
Source File: Storage.java    From android-storage with Apache License 2.0 5 votes vote down vote up
public long getFreeSpace(String dir, SizeUnit sizeUnit) {
    StatFs statFs = new StatFs(dir);
    long availableBlocks;
    long blockSize;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        availableBlocks = statFs.getAvailableBlocks();
        blockSize = statFs.getBlockSize();
    } else {
        availableBlocks = statFs.getAvailableBlocksLong();
        blockSize = statFs.getBlockSizeLong();
    }
    long freeBytes = availableBlocks * blockSize;
    return freeBytes / sizeUnit.inBytes();
}
 
Example 16
Source File: Utility.java    From Kernel-Tuner with GNU General Public License v3.0 5 votes vote down vote up
public static long getUsedSpaceInBytesOnInternalStorage()
{
    long usedSpace;
    StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
    usedSpace = ((long) stat.getBlockCount() - stat.getAvailableBlocks())
            * (long) stat.getBlockSize();

    return usedSpace;
}
 
Example 17
Source File: BaseUtils.java    From AndroidNetwork with MIT License 5 votes vote down vote up
/**
 * 获取SD卡剩余空间的大小
 * 
 * @return long SD卡剩余空间的大小(单位:byte)
 */
public static long getSDSize()
{
    final String str = Environment.getExternalStorageDirectory().getPath();
    final StatFs localStatFs = new StatFs(str);
    final long blockSize = localStatFs.getBlockSize();
    return localStatFs.getAvailableBlocks() * blockSize;
}
 
Example 18
Source File: ApiCompatibilityUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * See {@link android.os.StatFs#getAvailableBlocksLong}.
 */
@SuppressWarnings("deprecation")
public static long getAvailableBlocks(StatFs statFs) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return statFs.getAvailableBlocksLong();
    } else {
        return statFs.getAvailableBlocks();
    }
}
 
Example 19
Source File: Utils.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
public static long readSystemAvailableSize() {
	String path = "/data"; 
	StatFs sf = new StatFs(path);
	long blockSize = sf.getBlockSize();
	Log.d("block size", "block size: " + blockSize);
	//long blockCount = sf.getBlockCount();
	//Log.d("available count", "available count: " + sf.getAvailableBlocks());
	long availCount = sf.getAvailableBlocks();
	Log.d("available count", "available count: " + availCount);
	return blockSize * availCount/1024;
}
 
Example 20
Source File: ChatFileUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 3 votes vote down vote up
public static int freeSpaceOnSd() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
            .getPath());


    double sdFreeMB = ((double) stat.getAvailableBlocks() * (double) stat
            .getBlockSize()) / MB;


    return (int) sdFreeMB;
}