0

I am new in android and have question about Internal and External memory. I use 2 way to determine memory usage. First way is this:

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

public static String getTotalInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    long totalBlocks = stat.getBlockCount();
    return formatSize(totalBlocks * blockSize);
}

public static String getAvailableExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatSize(availableBlocks * blockSize);
    } else {
        return "ERROR";
    }
}

and the second one is:

public long InternalTotalMemory(){
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    long blockCount = statFs.getBlockCountLong();
    long blockSize = statFs.getBlockSizeLong();
    long total = blockCount * blockSize;
    return total;
}

public long InternalFreeMemory(){
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    long availableBlock = statFs.getAvailableBlocksLong();
    long blockSize = statFs.getBlockSizeLong();
    long free = availableBlock * blockSize;
    return free;
}

But I don't understand why they show different values. 1) Total internal memory 4 GB Available internal memory 4 GB

2) Total internal memory 991,898 MB Available internal memory 632,266 MB

Where is my mistake or calculation is wrong.

Skynet
  • 7,820
  • 5
  • 44
  • 80
qazxsw123
  • 11
  • 1

1 Answers1

0

This worked for me correctly, you shall use this one...

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
Salmaan
  • 3,543
  • 8
  • 33
  • 59
  • Where I can find Formatter.formatFileSize(this, availableBlocks * blockSize); Or you create it? There is no method formatFileSize()? – qazxsw123 Feb 13 '15 at 07:58