-1

I am trying to get the total internal storage of the device like user storage with device storage. i am able to get the internal device storage using this code.

final File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getBlockCount();
    long megAvailable = bytesAvailable / 1048576;

but the problem is that in my device i have 16GB internal memory and the result i am getting is 11GB. its not showing the System memory(where OS stored).

vishal jangid
  • 2,967
  • 16
  • 22

2 Answers2

0
 public static String getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return bytesToHuman(availableBlocks * blockSize);
    }

bytesToHuman can be read from this

    public static String floatForm (double d)
    {
        return new DecimalFormat("#.##").format(d);
    }


    public static String bytesToHuman(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 "???";
    }
Community
  • 1
  • 1
Htoo Aung Hlaing
  • 2,173
  • 15
  • 26
-1

You can use it like below

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long sdAvailSize = (long)stat.getAvailableBlocks() * (long)stat.getBlockSize();
long gigaAvailable = sdAvailSize / 1073741824;
Rajesh Gopu
  • 863
  • 9
  • 33