SD卡和系统存储空间的计算

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

需要用到的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

SDCardInfo类:

public class SDCardInfo {
    public long total;//总的大小

    public long free;//为使用的大小
}

StorageSize类:

public class StorageSize {
    public float value;
    public String suffix;//后缀
}

StorageUtil 类:

public class StorageUtil {
    //storage   G   M    K   B之间的转换
    public static String convertStorage(long size) {
        long kb=1024;
        long mb=kb*1024;
        long gb=mb*1024;

        if (size>=gb){
            return  String.format("%.1f GB", (float) size / gb);
        }else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else
            return String.format("%d B", size);

    }

    /**
     * 单位大小之间的转换
     * @param size
     * @return
     */
    public static StorageSize convertStorageSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        StorageSize sto = new StorageSize();
        if (size >= gb) {

            sto.suffix = "GB";
            sto.value = (float) size / gb;
            return sto;
        } else if (size >= mb) {

            sto.suffix = "MB";
            sto.value = (float) size / mb;

            return sto;
        } else if (size >= kb) {


            sto.suffix = "KB";
            sto.value = (float) size / kb;

            return sto;
        } else {
            sto.suffix = "B";
            sto.value = (float) size;

            return sto;
        }

    }

    /**
     * 得到SD卡的大小
     * @return
     */

    public static SDCardInfo getSDCardInfo() {
        // String sDcString = Environment.getExternalStorageState();

        if (Environment.isExternalStorageRemovable()) {
            String sDcString = Environment.getExternalStorageState();
            if (sDcString.equals(Environment.MEDIA_MOUNTED)) {
                File pathFile = Environment
                        .getExternalStorageDirectory();

                try {
                    StatFs statfs = new StatFs(
                            pathFile.getPath());

                    // 获取SDCard上BLOCK总数
                    long nTotalBlocks = statfs.getBlockCount();

                    // 获取SDCard上每个block的SIZE
                    long nBlocSize = statfs.getBlockSize();

                    // 获取可供程序使用的Block的数量
                    long nAvailaBlock = statfs.getAvailableBlocks();

                    // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)
                    long nFreeBlock = statfs.getFreeBlocks();

                    SDCardInfo info = new SDCardInfo();
                    // 计算SDCard 总容量大小MB
                    info.total = nTotalBlocks * nBlocSize;

                    // 计算 SDCard 剩余大小MB
                    info.free = nAvailaBlock * nBlocSize;

                    return info;
                } catch (IllegalArgumentException e) {

                }
            }
        }
        return null;
    }

    /**
     * 得到系统地空间大小
     * @param context
     * @return
     */

    public static SDCardInfo getSystemSpaceInfo(Context context) {
        File path = Environment.getDataDirectory();
        // File path = context.getCacheDir().getAbsoluteFile();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        long availableBlocks = stat.getAvailableBlocks();

        long totalSize = blockSize * totalBlocks;
        long availSize = availableBlocks * blockSize;
        SDCardInfo info = new SDCardInfo();
        info.total = totalSize;
        info.free = availSize;
        return info;


    }

    /**
     * 得到root的信息
     * @return
     */
    public static SDCardInfo getRootSpaceInfo() {
        File path = Environment.getRootDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        long availableBlocks = stat.getAvailableBlocks();

        long totalSize = blockSize * totalBlocks;
        long availSize = availableBlocks * blockSize;
        // 获取SDCard上每个block的SIZE
        long nBlocSize = stat.getBlockSize();

        SDCardInfo info = new SDCardInfo();
        // 计算SDCard 总容量大小MB
        info.total = totalSize;

        // 计算 SDCard 剩余大小MB
        info.free = availSize;
        return info;

    }
}

以上相关信息总结于:https://github.com/joyoyao/superCleanMaster 一键清理 开源版,包括内存加速,缓存清理,自启管理,软件管理等。

展开阅读全文

没有更多推荐了,返回首页