我怎样才能在Android的SD卡上的文件夹的大小? [英] How can I get the size of a folder on SD card in Android?

查看:104
本文介绍了我怎样才能在Android的SD卡上的文件夹的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能很容易地在SD卡上的文件夹的大小?我用一个文件夹的图像缓存,并想present所有缓存图像的总大小。有没有办法来此除了遍历每个文件?它们都位于同一个文件夹内?

Is it possible to easily get the size of a folder on the SD card? I use a folder for caching of images, and would like to present the total size of all cached images. Is there a way to this other than iterating over each file? They all reside inside the same folder?

推荐答案

刚刚经历的所有文件,总结它们的长度:

Just go through all files and sum the length of them:

/**
 * Return the size of a directory in bytes
 */
private static long dirSize(File dir) {

    if (dir.exists()) {
        long result = 0;
        File[] fileList = dir.listFiles();
        for(int i = 0; i < fileList.length; i++) {
            // Recursive call if it's a directory
            if(fileList[i].isDirectory()) {
                result += dirSize(fileList [i]);
            } else {
                // Sum the file size in bytes
                result += fileList[i].length();
            }
        }
        return result; // return the file size
    }
    return 0;
}

注:功能手写的,因此无法编译

NOTE: Function written by hand so it could not compile!

编辑:。递归调用固定

编辑:dirList.length改为fileList.length

EDITED: dirList.length changed to fileList.length.

这篇关于我怎样才能在Android的SD卡上的文件夹的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆