0

获取手机的内存详细信息

    //total
       StatFs statF = new StatFs(Environment.getDataDirectory().getAbsolutePath());

        float total = ((float)statF.getBlockCount() * statF.getBlockSize()) / 1073741824;
        TextView tt = (TextView) ax.findViewById(R.id.total1);
        tt.setText(String.valueOf(total)+"GB");

    //free
     StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());

       float free  = ((float)statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1073741824;
           TextView t = (TextView) ax.findViewById(R.id.free1);
           t.setText(String.valueOf(free)+"GB");
    //use
           float use = total - free ;
       TextView ttt = (TextView) ax.findViewById(R.id.use1);
           ttt.setText(String.valueOf(use)+"GB");

我正在使用此代码,但我得到 11.350227GB 的输出

4

2 回答 2

0

使用android.text.format.Formatter.formatFileSize()

于 2013-06-05T10:04:51.113 回答
0

没有直接的方法可以做到这一点,但您可以使用以下解决方案

private static final long K = 1024;
private static final long M = K * K;
private static final long G = M * K;
private static final long T = G * K;

public static String convertToStringRepresentation(final long value){
    final long[] dividers = new long[] { T, G, M, K, 1 };
    final String[] units = new String[] { "TB", "GB", "MB", "KB", "B" };
    if(value < 1)
        throw new IllegalArgumentException("Invalid file size: " + value);
    String result = null;
    for(int i = 0; i < dividers.length; i++){
        final long divider = dividers[i];
        if(value >= divider){
            result = format(value, divider, units[i]);
            break;
        }
     }
    return result;
}

private static String format(final long value,
    final long divider,
    final String unit){
    final double result =
        divider > 1 ? (double) value / (double) divider : (double) value;
    return new DecimalFormat("#,##0.#").format(result) + " " + unit;
}
于 2013-06-05T09:57:56.320 回答