Search code examples
androidsizestoragecapacityandroid-internal-storage

How to get exact capacity from storage


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

File Manager (Samsung) Storage (Settings)

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

Log


Solution

  • 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.