android - 为什么我得到的手机和 SD 卡内存信息不正确?

标签 android android-sdcard

我想从手机设置(例如 SDCART 和手机内存)中查看正确的信息。但是看到这个代码卡1800MB的总内存。我的卡是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();
}

最佳答案

来自 Environment#getExternalStorageDirectory()

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

有两个内部存储空间。一个用于应用程序和系统,一个用于数据。用于数据的就像第一张 SD 卡。 (这是一个非常低级的版本,请见谅。)也是调用getExternalStorageDirectory()方法得到的。

来源相同

In devices with multiple "external" storage directories, this directory represents the "primary" external storage that the user will interact with. Access to secondary storage is available through

我不是在开玩笑,这句话到此为止。我找到了另一个 answer这应该可以帮助您列出可用的“SD 卡”或外部存储。将其拆开并使用您需要的东西。我认为它看起来像这样:

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

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

关于android - 为什么我得到的手机和 SD 卡内存信息不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27662279/

相关文章:

android - 无法在android中以编程方式在 ScrollView 中添加线性布局

java - 安卓 : Copy RawFile to Sdcard (video mp4)

android - 移动 Apk 到 SD 卡

java - Sherlock Activity 中的 Intent.FLAG_ACTIVITY_CLEAR_TOP 未达到目的

java - 以编程方式正确打开设置中的 'About device' 页面?

android - 为什么有些应用无法移动到sd卡?

android - 使用 openFileOutput 时文件保存在哪里?

Android 音乐文件选择器 Intent 问题

java - Logcat 没有显示任何 android api level 22

android - 我如何创建带有撤消按钮的通知,例如 gmail?