zoukankan      html  css  js  c++  java
  • Android为TV端助力 计算每个目录剩余空间丶总空间以及SD卡剩余空间

    ublic class MemorySpaceCheck {
    /**
    * 计算剩余空间
    * @param path
    * @return
    */
    public static String getAvailableSize(String path)
    {
    StatFs fileStats = new StatFs(path);
    fileStats.restat(path);
    return String.valueOf(fileStats.getAvailableBlocks() * fileStats.getBlockSize()); // 注意与fileStats.getFreeBlocks()的区别
    // return getPrintSize((long) fileStats.getFreeBlocks() * fileStats.getFreeBlocks()); // 注意与fileStats.getFreeBlocks()的区别
    }
    /**
    * 跟上面的方法含义一样,返回值和方法名不同
    * @param path
    * @return
    */
    public static double getAvailableSizes(String path)
    {
    StatFs fileStats = new StatFs(path);
    fileStats.restat(path);
    return Double.valueOf(fileStats.getAvailableBlocks() * fileStats.getBlockSize()); // 注意与fileStats.getFreeBlocks()的区别
    // return getPrintSize((long) fileStats.getFreeBlocks() * fileStats.getFreeBlocks()); // 注意与fileStats.getFreeBlocks()的区别
    }
    /**
    * 计算总空间
    * @param path
    * @return
    */
    public static String getTotalSize(String path)
    {
    StatFs fileStats = new StatFs(path);
    fileStats.restat(path);
    return String.valueOf(fileStats.getBlockCount() * fileStats.getBlockSize());
    }

    /**
    * 计算SD卡的剩余空间
    * @return 剩余空间
    */
    public static String getSDAvailableSize()
    {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
    return getAvailableSize(Environment.getExternalStorageDirectory().toString());
    }

    return null;
    }

    /**
    * 计算系统的剩余空间
    * @return 剩余空间
    */
    public static String getSystemAvailableSize()
    {
    // context.getFilesDir().getAbsolutePath();
    return getAvailableSize("/data");
    }
    /**
    * 是否有足够的空间
    * @param filePath 文件路径,不是目录的路径
    * @return
    */
    public static boolean hasEnoughMemory(String filePath)
    {
    File file = new File(filePath);
    long length = file.length();
    if (filePath.startsWith("/sdcard") || filePath.startsWith("/mnt/sdcard"))
    {
    return Integer.parseInt(getSDAvailableSize()) > length;
    }
    else
    {
    return Integer.parseInt(getSystemAvailableSize()) > length;
    }

    }

    /**
    * 获取SD卡的总空间
    * @return
    */
    public static String getSDTotalSize()
    {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
    return getTotalSize(Environment.getExternalStorageDirectory().toString());
    }

    return null;
    }

    /**
    * 获取系统可读写的总空间
    * @return
    */
    public static String getSysTotalSize()
    {
    return getTotalSize("/data");
    }

    /**
    * 格式化单位
    * 转换为B,GB等等
    * @param size
    * @return
    */
    public static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
    return size + "Byte";
    }

    double megaByte = kiloByte / 1024;
    if (megaByte < 1) {
    BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
    return result1.setScale(1, BigDecimal.ROUND_HALF_UP)
    .toPlainString() + "KB";
    }

    double gigaByte = megaByte / 1024;
    if (gigaByte < 1) {
    BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
    return result2.setScale(1, BigDecimal.ROUND_HALF_UP)
    .toPlainString() + "MB";
    }

    double teraBytes = gigaByte / 1024;
    if (teraBytes < 1) {
    BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
    return result3.setScale(1, BigDecimal.ROUND_HALF_UP)
    .toPlainString() + "GB";
    }
    BigDecimal result4 = new BigDecimal(teraBytes);
    return result4.setScale(1, BigDecimal.ROUND_HALF_UP).toPlainString()
    + "TB";
    }

  • 相关阅读:
    storm学习之七-storm UI页面参数详解
    kafka学习之-KafkaOffsetMonitor后台监控
    hbase深入了解
    storm学习之六-使用Maven 生成jar包多种方式
    kafka学习之-集群配置及安装
    Python的Web应用框架--Django
    plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
    key-value数据库-Redis
    SUSE 12安装详解
    分布式网络文件系统--MooseFS
  • 原文地址:https://www.cnblogs.com/xiaoxiaing/p/6182621.html
Copyright © 2011-2022 走看看