AndroidTools: 系统工具--读取存储空间

Android Tools Git地址: https://github.com/wisesun7/AndroidTools.git

    本篇提供获取整机存储空间的方法,通过StatFs获取BlockCount和BlockSize计算各种空间。分别可获取当前剩余空间,系统占用空间,及Data分区的总空间,那么已用空间就可用总空间减去剩余空间得到。
    需要注意的是,这里的剩余可用、系统占用和总空间之间没有累计和绝对相等的关系,一般厂商会把总空间直接定义成固定值,而将剩余空间和系统占用空间用此方法获取真实值,然后通过【固定值-真实值】的方式展示已用空间。 这样做的好处是用户看到的总空间会和规格书中显示的一致,而剩余空间使用的是真实值,因此,在使用上也不会出现显示与实际使用不对应的关系。

/**
     * 获取当前可用空间
     * @return
     */
    public static float getAvailableSpace() {
        File file = Environment.getDataDirectory();
        StatFs sf = new StatFs(file.getPath());
        long blocks = sf.getAvailableBlocksLong();
        long blockSpace = sf.getBlockSizeLong();
        float space = (blocks * blockSpace * 1.0f) / (1024 * 1024 * 1024);
        Log.d(TAG, "getAvailableSpace: " + Math.round(space * 100) / 100);
        return (float) Math.round(space * 100) / 100;
    }

    /**
     * 获取系统占用空间
     * @return
     */
    public static float getSystemSpace() {
        File file = Environment.getRootDirectory();
        StatFs sf = new StatFs(file.getPath());
        long blocks = sf.getAvailableBlocksLong();
        long blockSpace = sf.getBlockSizeLong();
        float space = (blocks * blockSpace * 1.0f) / (1024 * 1024 * 1024);
        Log.d(TAG, "getAvailableSpace: " + Math.round(space * 100) / 100);
        return (float) Math.round(space * 100) / 100;
    }


    /**
     * 获取总空间(不包含系统占用)
     * @param context
     * @return
     */
    public static long getRomTotalSize(Context context) {
        File rom = Environment.getDataDirectory();
        StatFs statFs = new StatFs(rom.getPath());
        long totalSpace = statFs.getTotalBytes();
        return totalSpace;
    }
README for preparing SD/MMC/Micro-SD CARD for booting Android Pre-Requesites -------------- 1) Need to have an SD/MMC/Micro-SD card with atleast 2GB of size. 2) The script needs to be invoked from ubuntu linux machine 8.04 or above. 3) User needs to have sudo privileges. What script will do ------------------- The mkmmc-android.sh partitions the MMC/SD card into three partiions namely boot, rootfs and data. The script will then put the boot images on boot partition and extracts the android rootfs-rootfs_*.tar.bz2 to rootfs partition. Finally the script will copy the Media clips to the data partition and START_HERE folder to boot partition. How to invoke the script ------------------------ There are three ways of invoking this script. 1) Command: sudo ./mkmmc-android <device> <MLO> <u-boot.bin> <uImage> <boot.scr> <rootfs tar.bz2 > <Media_Clips> <START_HERE Location> Example: sudo ./mkmmc-android /dev/sdc MLO u-boot.bin uImage boot.scr rootfs.tar.bz2 Media_Clips Details: In this case, the script will take Boot Images (MLO,u-boot.bin, uImage and boot.scr) Root Filesystem tarball, Media Clips and START_HERE folder as arguements. The script will then put the boot images on boot partition, extract the android rootfs-rootfs_*.tar.bz2 to rootfs partition. Finally the script will copy the Media clips to the data partition and START_HERE folder to boot partition. 2) Command: sudo ./mkmmc-android <device> <MLO> <u-boot.bin> <uImage> <boot.scr> <rootfs tar.bz2 > Example: sudo ./mkmmc-android /dev/sdc MLO u-boot.bin uImage boot.scr rootfs.tar.bz2 Details: In this case, the script will take Boot Images (MLO,u-boot.bin, uImage and boot.scr) and Root Filesystem tarball as arguements. The script will then put the boot images on boot partition, extract the android rootfs-rootfs_*.tar.bz2 to rootfs partition. The data partiton will be left empty in this case. 3) Command: sudo ./mkmmc-android.sh <device> Example: sudo ./mkmmc-android.sh /dev/sdc Details: In this case, the script will assume default locations for BootImages, Root Filesystem, Media_Clips and START_HERE. This command is equivalent to the following sudo ./mkmmc-android /dev/sdc Boot_Images/MLO Boot_Images/u-boot.bin Boot_Images/uImage Boot_Images/boot.scr Filesystem/rootfs.tar.bz2 Media_Clips START_HERE If you are in a particular Board Specific Directory, extracted from DevKit Release, then you can prepare the sd card by executing sudo ./mkmmc-android.sh <device>, to prepare sd card.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值