为什么我的电话和SD卡内存信息不正确?

时间:2014-12-26 21:44:48

标签: android android-sdcard

我想从手机设置中查看正确的信息,例如SDCART和手机内存。但看到这个代码卡1800 MB总内存。我的卡是16GB。无法弄清楚。我尝试了很多代码,但没有。我该如何解决这个问题。

  

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.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.getAbsolutePath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatSize(availableBlocks * blockSize);
    } else {
        return ERROR;
    }
}

public static String getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
       /* File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getAbsolutePath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return formatSize(totalBlocks * blockSize);
     

* /               StatFs stat = new StatFs(Environment.getExternalStorageDirectory()。getPath());               long bytesAvailable =(long)stat.getBlockSize()*(long)stat.getAvailableBlocks();               long megAvailable = bytesAvailable /(1024 * 1024);               return formatSize((long)stat.getBlockSize()*(long)stat.getAvailableBlocks());

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

1 个答案:

答案 0 :(得分:2)

来自Environment#getExternalStorageDirectory()

  

注意:不要被"外部"这个词弄糊涂。这里。此目录最好被视为媒体/共享存储。它是一个文件系统,可以容纳相对大量的数据,并在所有应用程序之间共享(不强制执行权限)。传统上这是一张SD卡,但它也可以作为内置存储器实现,该设备不同于受保护的内部存储器,可以作为文件系统安装在计算机上。

有两个内部存储空间。一个用于应用程序和系统,一个用于数据。数据的行为就像第一张SD卡。 (这是非常愚蠢的版本,请原谅我。)这也是你通过调用getExternalStorageDirectory()方法获得的那个。

来自同一个来源

  

在具有多个"外部"的设备中存储目录,这个目录代表" primary"用户将与之交互的外部存储。可通过

访问二级存储

我不是在开玩笑,句子在那里结束。我找到了另一个answer,它可以帮助你列出可用的" SD卡"或外部存储。将它拆开并使用你需要的东西。我认为它看起来像这样:

String externals = System.getenv("SECONDARY_STORAGE");
String[] externalsArray = externals.split(":");
String secondaryExternal = externalsArray[0];
File path = new File(secondaryExternal);

请注意,我没有测试过上述内容。