无法获得设备总存储空间

时间:2015-09-30 12:53:50

标签: android android-external-storage

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

enter image description here

enter image description here

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

如何获得总空间?

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()是从字节中获取可读字符串。

1 个答案:

答案 0 :(得分:0)

我认为你将获得内部存储路径。

请使用以下方法获取外部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;
    }