我正在尝试使用设备存储来获取设备的总内部存储量,例如用户存储。我可以使用此代码获取内部设备存储空间。
final File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
但问题是在我的设备中我有16GB的内存,我得到的结果是11GB。它没有显示系统内存(OS存储的位置)。
答案 0 :(得分:0)
public static String getAvailableInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return bytesToHuman(availableBlocks * blockSize);
}
bytesToHuman可以从this
中读取 public static String floatForm (double d)
{
return new DecimalFormat("#.##").format(d);
}
public static String bytesToHuman(long size)
{
long Kb = 1 * 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 ) + " byte";
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 "???";
}
答案 1 :(得分:-1)
您可以像下面那样使用它
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long sdAvailSize = (long)stat.getAvailableBlocks() * (long)stat.getBlockSize();
long gigaAvailable = sdAvailSize / 1073741824;