无法获取设备总存储空间

Can´t get device total storage

我有一个设备(外部存储 + SD 卡):

使用下一个代码我得到 8GB 而不是 31GB,为什么?

我怎样才能获得总 space 可用?

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

formatSize() 是从字节中获取可读字符串。

我想你会得到内部存储路径。

请使用下面的方法获取外部SD卡路径然后你会使用你的方法希望它能帮助你

private String getAvailableExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = new File(getExternalSdCardPath());
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return formatSize(availableBlocks * blockSize);
    } else {
        return "Error";
    }
}


public static String getExternalSdCardPath() throws Throwable
    {
        String m_externalPath;
        m_externalPath = System.getenv("SECONDARY_STORAGE");
        return m_externalPath;
    }