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());
}
2. 获得文件总大小
public static long getTotalSizeOf(final String storagePath) {
if (TextUtils.isEmpty(storagePath)) {
return 0;
}
// 尝试多加判断,如果无效的参数 StatFs 会报错
File file = new File(storagePath);
boolean isSymLink = false;
try {
isSymLink = FileUtil.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;
}
3. 可以获得的大小
public static long getAvailableSizeOf(final String storagePath) {
if (TextUtils.isEmpty(storagePath)) {
return 0;
}
// 尝试多加判断,如果无效的参数 StatFs 会报错
File file = new File(storagePath);
boolean isSymLink = false;
try {
isSymLink = FileUtil.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 availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
}