我已经阅读了有关此主题的其他问题,但我找不到解决方案,因为没有任何对我有用。
我正在尝试获取总设备磁盘空间,无论它是空闲还是忙碌。 当您看到设备规范时,会出现"存储"通常为64GB,32GB或8GB的项目,表示整个设备存储。
到目前为止,我的代码是:
private static String floatForm(double d) {
return new DecimalFormat("#.##").format(d);
}
@NonNull
private static String bytesToHuman(long size) {
long Kb = 1024;
long Mb = Kb * 1024;
long Gb = Mb * 1024;
long Tb = Gb * 1024;
long Pb = Tb * 1024;
long Eb = Pb * 1024;
if (size < Kb) return floatForm(size) + " Bytes";
if (size >= Kb && size < Mb) return floatForm((double) size / Kb) + " KB";
if (size >= Mb && size < Gb) return floatForm((double) size / Mb) + " MB";
if (size >= Gb && size < Tb) return floatForm((double) size / Gb) + " GB";
if (size >= Tb && size < Pb) return floatForm((double) size / Tb) + " TB";
if (size >= Pb && size < Eb) return floatForm((double) size / Pb) + " PB";
if (size >= Eb) return floatForm((double) size / Eb) + " EB";
return "???";
}
static public String totalSpace() {
long availableBytes = 0;
StatFs root = new StatFs(Environment.getRootDirectory().getPath());
StatFs data = new StatFs(Environment.getDataDirectory().getPath());
availableBytes += root.getBlockCountLong() * root.getBlockSizeLong();
availableBytes += data.getBlockCountLong() * data.getBlockSizeLong();
return bytesToHuman(availableBytes);
}
在我的OnePlus 5(128GB)上,它返回114,17GB,在我的三星J5(8GB)6,65GB上。它总是缺少10-20%的大小
我如何修复此代码?
答案 0 :(得分:0)
public static long getTotalInternalMemorySize() {
final File path = Environment.getDataDirectory();
final StatFs stat = new StatFs(path.getPath());
final long blockSize = stat.getBlockSize();
final long totalBlocks = stat.getBlockCount();
long sizeInMb = totalBlocks * blockSize / (1024 * 1024);
return sizeInMb;
}
Try this. If not suitable for you let me know. Thanks