Android应用内存使用情况

时间:2015-02-13 07:07:18

标签: java android

我是android的新手,对内部和外部内存有疑问。我使用2种方式来确定内存使用情况。 第一种方式是:

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

,第二个是:

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

但我不明白为什么他们表现出不同的价值观。 1)总内存4 GB    可用内部存储器4 GB

2)总内存991,898 MB    可用内部存储器632,266 MB

我的错误或计算错误在哪里。

1 个答案:

答案 0 :(得分:0)

这对我有用,你应该使用这个......

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);