我想以编程方式从实习生存储中读取确切的容量。
我使用的是三星 Galaxy S7 Edge 32GB 设备。
在预装的三星文件管理器(德语:Eigene Dateien)中,它显示了 32GB 的总容量。即使在菜单 => 设置 => 存储中,它也会显示 32GB。
(显示在屏幕截图中)
当我使用下面的代码时,它显示我的总容量为 24.4GB。
(显示在帖子末尾的日志中)
问题:如何获得我设备的确切 32GB 总容量?
截图
完整代码:
package spicysoftware.com.space;
import android.os.Environment;
import android.os.StatFs;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String InternalFreeSpace = convertBytes(getInternalFreeSpace());
Log.v("InternalFreeSpace : ", InternalFreeSpace);
String InternalTotalSpace = convertBytes(getInternalTotalSpace());
Log.v("InternalTotalSpace : ", InternalTotalSpace);
String InternalUsedSpace = convertBytes(getInternalUsedSpace());
Log.v("InternalUsedSpace : ", InternalUsedSpace);
}
public long getInternalFreeSpace() {
//Get free Bytes...
long bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
return bytesAvailable;
}
public long getInternalTotalSpace() {
//Get total Bytes
long bytesTotal = (stat.getBlockSizeLong() * stat.getBlockCountLong());
return bytesTotal;
}
public long getInternalUsedSpace() {
//Get used Bytes
long bytesUsed = getInternalTotalSpace() - getInternalFreeSpace();
return bytesUsed;
}
public static String convertBytes (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 "anything...";
}
public static String floatForm (double d) {
return new DecimalFormat("#.##").format(d);
}
}
日志