Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get SD Card size in Android?

Welcome all

I have tried every question related to this in Stackoverflow and google and none of them works. I have tried something like this next link, but it returns the same as internal storage: How to get an External storage sd card size (With Mounted SD card)?

For example, if I have about 12GB internal storage and 4GB SD card storage, no matter what method I use, I always get the exact same number for the SD space as I do for internal space.

It seems that the old methods posted here in Stackoverflow only works until Android KitKat but do not work in next android versions.

Is possible to solve this?

like image 228
NullPointerException Avatar asked Sep 29 '16 18:09

NullPointerException


People also ask

How do I find out how big my SD card is on my Android?

Go to Settings > Storage and scroll down to the bottom. There you'll find SD card total storage and storage used.

How do I know how big my SD card is?

Install the H2testw on your Windows computer (you can also run a similar tool called “F3” on Mac OS, and an app called SD Insight on Android device) and run the test, it will show you the real capacity of your SD card.


1 Answers

Ok, I've always wondered this and couldn't find an answer online. So here's what I do. It might not be so clean but it works for me every time.

For my case: it returns 61,055 MB. I have a 64 GB sd card inserted.

Oh and I forgot to mention: I did this on Samsung Galaxy S5 6.0 and Sony Xperia Z5 Premium 5.1.1 today to confirm. However, I also have an app that few hundred people use daily and I haven't experienced any issues yet.

@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();
}
like image 190
ᴛʜᴇᴘᴀᴛᴇʟ Avatar answered Oct 13 '22 11:10

ᴛʜᴇᴘᴀᴛᴇʟ