获取设备总内部内存大小

时间:2018-06-08 06:55:45

标签: android memory system

我的HTC设备内部手机内存大小为16 GB,但以下代码仅显示14GB。 我想找到设备的总内存大小。以下代码仅显示用户可用的内部存储器大小,而不是系统分配的大小。

    File path = Environment.getRootDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSizeLong();
    long totalBlocks = stat.getBlockCountLong();
    long rootCount=totalBlocks * blockSize;
    Log.e("getRootDirectory",humanReadableByteCount(rootCount,true));

    File dataPath = Environment.getDataDirectory();
    StatFs dataStat = new StatFs(dataPath.getPath());
    long dataCount=dataStat.getBlockCountLong() * 
    dataStat.getBlockSizeLong();
    Log.e("getDataDirectory",humanReadableByteCount(dataCount,true));

    File dwnldPath = Environment.getDownloadCacheDirectory();
    StatFs dwnldStat = new StatFs(dwnldPath.getPath());
    long dwnldCount=dwnldStat.getBlockCountLong() * 
    dwnldStat.getBlockSizeLong();

         /*
    File extPath = Environment.getExternalStorageDirectory();
    StatFs extStat = new StatFs(extPath.getPath());
    long extCount=extStat.getBlockCountLong() * 
   extStat.getBlockSizeLong();

    */
    long total=rootCount+dataCount+dwnldCount;

任何人都知道如何找到设备的确切内存大小(手机内存大小+系统区域大小)

2 个答案:

答案 0 :(得分:0)

尝试这种方式来寻找记忆

MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;

答案 1 :(得分:0)

使用下面的代码,您将获得内部,外部以及ram记忆:

public static boolean externalMemoryAvailable() {
    return android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED);
}

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

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

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

public static String getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long totalBlocks = stat.getBlockCountLong();
        return formatSize(totalBlocks * blockSize);
    } else {
        return ERROR;
    }
}

public static String formatSize(long size) {
    String suffix = null;

    if (size >= 1024) {
        suffix = "KB";
        size /= 1024;
        if (size >= 1024) {
            suffix = "MB";
            size /= 1024;
        }
    }

    StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

    int commaOffset = resultBuffer.length() - 3;
    while (commaOffset > 0) {
        resultBuffer.insert(commaOffset, ',');
        commaOffset -= 3;
    }

    if (suffix != null) resultBuffer.append(suffix);
    return resultBuffer.toString();
}

获取RAM大小

ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;