【问题标题】:How to get free space available in Internal and External storages [duplicate]如何在内部和外部存储中获得可用空间 [重复]
【发布时间】:2015-08-11 11:37:20
【问题描述】:

如何在内部和外部存储中获得可用空间?我正在开发一个下载 ROM 的应用程序,我必须检查是否有足够的可用空间来下载它。谁能帮我?提前致谢!

【问题讨论】:

    标签: java android storage


    【解决方案1】:

    根据用户79758:

    这是我正在使用的功能:

    public static float megabytesAvailable(File f) {
        StatFs stat = new StatFs(f.getPath());
        long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
        return bytesAvailable / (1024.f * 1024.f);
    }
    

    以上代码引用了一些截至 2014 年 8 月 13 日已弃用的功能。我在下面复制了一个更新版本:

    public static float megabytesAvailable(File f) {
        StatFs stat = new StatFs(f.getPath());
        long bytesAvailable = (long)stat.getBlockSizeLong() * (long)stat.getAvailableBlocksLong();
        return bytesAvailable / (1024.f * 1024.f);
    }
    

    【讨论】:

    • 太好了,复制然后再复制
    • 它对我不起作用!
    【解决方案2】:

    这是您在 android 中检查内存状态的一种方法。我已经在我的应用程序中实现了。

    public class AvailableSpaceHandler {
    
        //*********
        //Variables
        /**
         * Number of bytes in one KB = 2<sup>10</sup>
         */
        private final static long SIZE_KB = 1024L;
    
        /**
         * Number of bytes in one MB = 2<sup>20</sup>
         */
        private final static long SIZE_MB = SIZE_KB * SIZE_KB;
    
        /**
         * Number of bytes in one GB = 2<sup>30</sup>
         */
        private final static long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;
    
        //********
        // Methods
    
        /**
         * @return Number of bytes available on external storage
         */
        private static long getExternalAvailableSpaceInBytes(int newAndroidAPI) {
            long availableSpace = -1L;
            try {
                StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
                if (newAndroidAPI >= Build.VERSION_CODES.KITKAT) {
                    availableSpace = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
                } else {
                    availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return availableSpace;
        }
    
    
        /**
         * @return Number of kilo bytes available on external storage
         */
        public static long getExternalAvailableSpaceInKB(int newAndroidAPI) {
            return getExternalAvailableSpaceInBytes(newAndroidAPI) / SIZE_KB;
        }
    
        /**
         * @return Number of Mega bytes available on external storage
         */
        public static long getExternalAvailableSpaceInMB(int newAndroidAPI) {
            return getExternalAvailableSpaceInBytes(newAndroidAPI) / SIZE_MB;
        }
    
        /**
         * @return gega bytes of bytes available on external storage
         */
        public static long getExternalAvailableSpaceInGB(int newAndroidAPI) {
            return getExternalAvailableSpaceInBytes(newAndroidAPI) / SIZE_GB;
        }
    
        /**
         * @return Total number of available blocks on external storage
         */
        public static long getExternalStorageAvailableBlocks(int newAndroidAPI) {
            //newAndroidAPI to check API is < KITKAT
            long availableBlocks = -1L;
            try {
                StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
                if (newAndroidAPI >= Build.VERSION_CODES.KITKAT) {
                    availableBlocks = stat.getAvailableBlocksLong();
                } else {
                    availableBlocks = stat.getAvailableBlocks();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return availableBlocks;
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2011-06-03
    • 2019-03-02
    • 2011-12-29
    • 2011-03-10
    • 1970-01-01
    • 2015-11-12
    • 2011-11-28
    • 1970-01-01
    • 2011-03-12
    相关资源
    最近更新 更多