如何从存储中获取准确的容量

MSe*_*iz5 3 size storage android capacity android-internal-storage

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

我使用的是 Samsung 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

日志

日志

Muk*_*osh 5

1.首先,打开命令提示符。在开发者选项打开的情况下连接您的手机。在 cmd 上写入"adb shell"。您现在将位于手机路径中。2.其次,df -h在cmd上写入,这会显示手机总存储被划分到不同的磁盘路径。

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));`
Run Code Online (Sandbox Code Playgroud)

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