前言

代码功能:获取Sdcard或者U盘磁盘大小,空闲大小等。

好记性不如烂笔头

正文

存在单位转换

    /**
     * 磁盘单位转换
     *
     * @param size
     * @return
     */
    public static String unitConversion(long size) {
        long kb = 1024;
        long mb = kb << 10;
        long gb = mb << 10;
        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);
        }

android SDK < 2.3

    /**
     * 文件系统上可用的空闲块的数量
     *
     * @param path
     * @return
     */
    public static long getAvailSize(String path) {
        if (TextUtils.isEmpty(path)) {
            return -1;
        }
        try {
            StatFs statFs = new StatFs(path);
            if (null != statFs) {
                return statFs.getBlockSizeLong() * statFs.getAvailableBlocksLong();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }


    /**
     * 文件系统上空闲的块总数,包括预留块
     *
     * @param path
     * @return
     */
    public static long getFreeSize(String path) {
        if (TextUtils.isEmpty(path)) {
            return -1;
        }
        try {
            StatFs statFs = new StatFs(path);
            if (null != statFs) {
                return statFs.getBlockSizeLong() * statFs.getFreeBlocksLong();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * @param path
     * @return
     */
    public static long getTotalSize(String path) {
        if (TextUtils.isEmpty(path)) {
            return -1;
        }
        try {
            StatFs statFs = new StatFs(path);
            if (null != statFs) {
                return statFs.getBlockSizeLong() * statFs.getBlockCountLong();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

android SDK > 2.3

    /**
     * @param path
     * @return
     */
    public static long getAvailSize2(String path) {
        if (TextUtils.isEmpty(path)) {
            return -1;
        }
        try {
            File dir = new File(path);
            return dir.getUsableSpace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * @param path
     * @return
     */
    public static long getFreeSize2(String path) {
        if (TextUtils.isEmpty(path)) {
            return -1;
        }
        try {
            File dir = new File(path);
            return dir.getFreeSpace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * @param path
     * @return
     */
    public static long getTotalSize2(String path) {
        if (TextUtils.isEmpty(path)) {
            return -1;
        }
        try {
            File dir = new File(path);
            return dir.getTotalSpace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

参考文章

  1. 获取android SDCard存储大小
  2. android 获得指定路径下可用存储大小

相关文章

暂无评论

none
暂无评论...