Android中StatFs获取存储空间信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhe_ge_sha_shou/article/details/89399465

StatFs类

这里要介绍下StatFs这个类,StatFs位于android.os包下,功能是:检索有关文件系统上空间的总体信息。
我们先来看下StatFs都提供了下列方法(废弃的方法这里就不列出了):

方法名 功能
getBlockSizeLong 文件系统上块的大小(以字节为单位)
getBlockCountLong 文件系统上的块总数
getFreeBlocksLong 文件系统上可用的块的总数,包括保留块(对普通应用程序不可用),大多数应用程序应使用getAvailableBlocksLong方法
getFreeBytes 文件系统上可用的字节数,包括保留的字节数块(正常应用程序不可用)。大多数应用程序应使用getAvailableBytes方法
getAvailableBlocksLong 文件系统上可用的块的数目
getAvailableBytes 文件系统上可用的字节数
getTotalBytes 文件系统支持的字节总数

StatFs类构造方法中要传入路径,通过Os.statvfs(path)返回StructStatVfs对象,所有的方法调用都是调用这个返回对象的。

StructStatVfs类

StructStatVfs类封装了文件信息

public final class StructStatVfs {
    /** 文件系统块大小 */
    public final long f_bsize; /*unsigned long*/

    /** 基本文件系统块大小 */
    public final long f_frsize; /*unsigned long*/

    /** 总块计数 */
    public final long f_blocks; /*fsblkcnt_t*/

    /** 可用块计数 */
    public final long f_bfree; /*fsblkcnt_t*/

    /** 非根可用的可用块计数 */
    public final long f_bavail; /*fsblkcnt_t*/

    /** 文件总数 */
    public final long f_files; /*fsfilcnt_t*/

    /** 空闲文件计数 */
    public final long f_ffree; /*fsfilcnt_t*/

    /** 非根目录可用的空闲文件计数 */
    public final long f_favail; /*fsfilcnt_t*/

    /** 文件系统标识 */
    public final long f_fsid; /*unsigned long*/

    /** Bit mask of ST_* flags. */
    public final long f_flag; /*unsigned long*/

    /** 最大文件名长度 */
    public final long f_namemax; /*unsigned long*/

    /**
     * Constructs an instance with the given field values.
     */
    public StructStatVfs(long f_bsize, long f_frsize, long f_blocks, long f_bfree, long f_bavail,
            long f_files, long f_ffree, long f_favail,
            long f_fsid, long f_flag, long f_namemax) {
        this.f_bsize = f_bsize;
        this.f_frsize = f_frsize;
        this.f_blocks = f_blocks;
        this.f_bfree = f_bfree;
        this.f_bavail = f_bavail;
        this.f_files = f_files;
        this.f_ffree = f_ffree;
        this.f_favail = f_favail;
        this.f_fsid = f_fsid;
        this.f_flag = f_flag;
        this.f_namemax = f_namemax;
    }

    @Override public String toString() {
        return Objects.toString(this);
    }
}

获取SD卡全部存储空间和可用存储空间

public final class FileUtils {

    public static final int BLOCK_SIZE = 1024;

    // 获取SD卡全部存储空间
    public static long getTotalSize() {
        File file = Environment.getExternalStorageDirectory();
        StatFs statFs = new StatFs(file.getPath());
        //获得sdcard上 block的总数
        long blockCount = statFs.getBlockCountLong();
        //获得sdcard上每个block 的大小
        long blockSize = statFs.getBlockSizeLong();
        //计算标准大小使用:1024,当然使用1000也可以
        return blockCount * blockSize / BLOCK_SIZE / BLOCK_SIZE;
    }

    // 获取SD卡可用存储空间
    public static long getAvailableSize() {
        File file = Environment.getExternalStorageDirectory();
        StatFs statFs = new StatFs(file.getPath());
        //获得sdcard上 block的总数
        long blockCount = statFs.getAvailableBlocksLong();
        //获得sdcard上每个block 的大小
        long blockSize = statFs.getBlockSizeLong();
        //计算标准大小使用:1024,当然使用1000也可以
        return blockCount * blockSize / BLOCK_SIZE / BLOCK_SIZE;
    }
}

猜你喜欢

转载自blog.csdn.net/zhe_ge_sha_shou/article/details/89399465