有没有办法在Android中获得SD卡大小?

时间:2016-09-29 18:09:38

标签: android storage android-sdcard sd-card

欢迎所有

我在Stackoverflow和google中尝试了与此相关的所有问题,但没有一个能够正常工作。我尝试过类似下一个链接,但它返回内部存储:How to get an External storage sd card size (With Mounted SD card)?

例如,如果我有大约12GB的内部存储空间和4GB的SD卡存储空间,无论我使用什么方法,我总是得到与内部空间完全相同的SD空间数。

似乎在Stackoverflow中发布的旧方法仅适用于Android KitKat,但在下一个Android版本中不起作用。

有可能解决这个问题吗?

2 个答案:

答案 0 :(得分:4)

好的,我一直都想知道这一点,并且无法在线找到答案。所以这就是我的工作。它可能不是那么干净但每次都适合我。

对于我的情况:它返回61,055 MB。我插入了64 GB的SD卡。

哦,我忘了提及:我今天在三星Galaxy S5 6.0 Sony Xperia Z5 Premium 5.1.1 上做了这个确认。但是,我也有一个应用程序,每天有几百人使用,我还没有遇到任何问题。

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static String getExternalSdCardSize() {
    File storage = new File("/storage");
    String external_storage_path = "";
    String size = "";

    if (storage.exists()) {
        File[] files = storage.listFiles();

        for (File file : files) {
            if (file.exists()) {
                try {
                    if (Environment.isExternalStorageRemovable(file)) {
                        // storage is removable
                        external_storage_path = file.getAbsolutePath();
                        break;
                    }
                } catch (Exception e) {
                    Log.e("TAG", e.toString());
                }
            }
        }
    }

    if (!external_storage_path.isEmpty()) {
        File external_storage = new File(external_storage_path);
        if (external_storage.exists()) {
            size = totalSize(external_storage);
        }
    }
    return size;
}

private static String totalSize(File file) {
    StatFs stat = new StatFs(file.getPath());
    long blockSize, totalBlocks;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        blockSize = stat.getBlockSizeLong();
        totalBlocks = stat.getBlockCountLong();
    } else {
        blockSize = stat.getBlockSize();
        totalBlocks = stat.getBlockCount();
    }

    return formatSize(totalBlocks * blockSize);
}

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

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

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

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

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

答案 1 :(得分:0)

我的手机内置32GB存储空间和15GB SD卡。在df上执行/mnt/sdcard会得到32GB的结果,这不是我们想要的结果。进一步挖掘,我找到了这个/storage目录。它有3个文件:

shell@E5803:/storage $ ls
8E5D-12E2
emulated
self

对每个项目执行df会得到以下结果:

shell@E5803:/storage $ df self                                                 
Filesystem               Size     Used     Free   Blksize
self                   889.4M     0.0K   889.4M   4096
shell@E5803:/storage $ df emulated                                             
Filesystem               Size     Used     Free   Blksize
emulated                22.6G    10.8G    11.8G   4096
shell@E5803:/storage $ df 8E5D-12E2/                                           
Filesystem               Size     Used     Free   Blksize
8E5D-12E2/              14.9G     2.3G    12.7G   32768

我认为魔术命令是"df /storage/" + mFilesArray[0],其中mFilesArray[]ls /storage的结果。

(让我们希望Google员工不会在将来改变SD卡挂载点,我怀疑。)

希望这有帮助。

相关问题