3

我想以编程方式从实习生存储中读取确切的容量。

我使用的是三星 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);
    }
}

日志

日志

4

2 回答 2

3

当我使用下面的代码时,它显示我的总容量为 24.4GB。

那是分区上包含的空间量Environment.getExternalStorageDirectory()。设备上会有其他分区,您将无法访问这些分区。

如何获得我设备的确切 32GB 总容量?

没有办法做到这一点,除非可能在有根设备上。

于 2017-03-24T16:49:46.350 回答
2

1.首先,打开命令提示符。在开发人员选项打开的地方连接您的手机。在 cmd 上写入"adb shell"。您现在将在您的电话路径中。2.其次,在cmd上写入df -h,显示手机总存储空间分为不同的磁盘路径。

3.看这张图。在此处输入图像描述

  `  double size =
                    new File("/data").getTotalSpace() / (1024.0 * 1024 * 1024);


            double sizesystem =
                    new File("/system").getTotalSpace() / (1024.0 * 1024 * 1024);

            double sizedev =
                    new File("/dev").getTotalSpace() / (1024.0 * 1024 * 1024);

            double sizecache =
                    new File("/cache").getTotalSpace() / (1024.0 * 1024 * 1024);

            double sizemnt =
                    new File("/mnt").getTotalSpace() / (1024.0 * 1024 * 1024);

            double sizevendor =
                    new File("/vendor").getTotalSpace() / (1024.0 * 1024 * 1024);



            float total = (float) (sizemnt + sizedev + sizesystem + size + sizevendor + sizecache);
            Log.e("total111", String.valueOf(total));`

在这里,使用所有的盘子路径并获得大小。然后将所有内容汇总并获得手机的实际存储空间。iF 手机是 32 GB,您将看到 32 GB 的总存储空间。

于 2019-10-19T04:00:28.670 回答