获取系统/sdcard存储空间路径无效的处理

来源:互联网 发布:linux集成开发环境 编辑:程序博客网 时间:2024/05/21 15:51
转载至http://m.blog.csdn.net/article/details?id=18086311
在公司项目中遇到的问题,做个笔记
获取总的大小:
public static long getTotalSizeOf(final String storagePath) {if (TextUtils.isEmpty(storagePath)) {return 0;}// 尝试多加判断,如果无效的参数 StatFs 会报错File file = new File(storagePath);boolean isSymLink = false;try {isSymLink = isSymlink(file);} catch (IOException e) {e.printStackTrace();}if (!file.exists() || !file.isDirectory() || isSymLink) {return 0;}StatFs stat = new StatFs(storagePath);long blockSize = stat.getBlockSize();long blockCount = stat.getBlockCount();return blockCount * blockSize;}

public static boolean isSymlink(File file) throws IOException {        if (file == null) {            throw new NullPointerException("File must not be null");        }                File canon;        if (file.getParent() == null) {            canon = file;        } else {            File canonDir = file.getParentFile().getCanonicalFile();            canon = new File(canonDir, file.getName());        }        return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());    }

主要是StatFs stat = new StatFs(storagePath);这句报错,需要做出判断处理,代码如上



0 0
原创粉丝点击