Is there a way to get the size of an SD card in Android?

Welcome everyone

I tried every question related to this on Stackoverflow and google, and none of them work. I tried something like this following link, but it returns the same as the internal storage: How do I get the size of the external SD storage card (with the SD card installed)?

For example, if I have about 12 GB of internal memory and 4 GB of SD card memory, no matter which method I use, I always get the same number for the SD space as for the internal space.

It seems that the old methods posted here on Stackoverflow only work until Android KitKat, but do not work on future versions of Android.

Is it possible to solve this?

+6
android storage sd-card android-sdcard
Sep 29 '16 at 18:09
source share
2 answers

Well, I always asked this question and could not find the answer on the Internet. So here is what I do. It may not be so clean, but it works for me every time.

In my case: it returns 61,055 MB. I have a 64 gigabyte SD card installed.

Oh, and I forgot to mention: I did it on the Samsung Galaxy S5 6.0 and Sony Xperia Z5 Premium 5.1.1 today for confirmation. However, I also have an application in which several hundred people work daily, and I have not experienced any problems 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(); } 
+4
Sep 29 '16 at 20:07
source share

My phone has a built-in 32 GB memory and a 15 GB SD card. Running df on /mnt/sdcard gives a 32 GB result, which we are not looking for. I dig further, I found this /storage directory. There are 3 files there:

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

doing df for each element gives the following:

 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 

I think the magic command is "df /storage/" + mFilesArray[0] , where mFilesArray[] is the result of ls /storage .

(I hope that Google users will not change the connection point of the SD card in the future, which I doubt.)

Hope this helps.

0
Sep 29 '16 at 20:31
source share



All Articles