How to get exact capacity from storage

醉酒当歌 提交于 2019-12-08 06:19:36

问题


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

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

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.

(Showed in screenshots)

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

(Showed in Log at the end of the post)

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

Screenshots

Full Code:

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

Log


回答1:


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

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.

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

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




回答2:


1.First ,open command prompt. Connect your mobile where developer option is on. Write "adb shell" on cmd.You will be now in your phone path. 2.Secondly, write df -h on cmd, which show you the total phone storage is divided into different disk path.

3.See this image.enter image description here

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

Here, usE the all dish path and get the size.Then sum up all and get the actual storage of a phone. iF phone is 32 GB, you will see the 32 GB total storage.



来源:https://stackoverflow.com/questions/43004556/how-to-get-exact-capacity-from-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!