如何从存储中获取准确的容量 [英] How to get exact capacity from storage

查看:104
本文介绍了如何从存储中获取准确的容量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过编程方式从内部存储中读取确切的容量。

I want to read the exact capacity from the intern storage programmatically.

我使用的是三星Galaxy S7 Edge 32GB设备。

I's a Samsung Galaxy S7 Edge 32GB device what I am using.

在预装的Samsung FileManager(德语:Eigene Dateien)中,它显示了32GB的总容量。
即使在菜单=>设置=>存储中,它也显示32GB。

In the preinstalled Samsung FileManager (In German: Eigene Dateien) it shows me the total capacity of 32GB. Even in the menu => settings => Storage it shows me 32GB.

(在屏幕截图中显示)

当我使用下面的代码时,它的总容量为24, 4GB。

When I use my code below it shows me a total capacity of 24,4GB.

(显示在帖子末尾的日志中)

(Showed in Log at the end of the post)

问题:如何获取设备的确切32GB总容量?

Question: How to get the exact 32GB total capacity of my device?

屏幕截图

完整代码:

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);
    }
}

日志

推荐答案


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

When I use my code below it shows me a total capacity of 24,4GB.

包含 Environment.getExternalStorageDirectory()的分区。设备上还会有其他分区,您将无法访问。

That is the amount of space on the partition that contains Environment.getExternalStorageDirectory(). There will be other partitions on the device, for which you will not have access.


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

How to get the exact 32GB total capacity of my device?

除了可能在有根设备上,没有其他方法可以这样做。

There is no means of doing that, except perhaps on a rooted device.

这篇关于如何从存储中获取准确的容量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆