How to Get the Size of a Folder on Sd Card in Android

How to get actual size of mounted SD card in Android?

Yes, there is a way.

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long sdAvailSize = (long)stat.getAvailableBlocks()
* (long)stat.getBlockSize();
//One binary gigabyte equals 1,073,741,824 bytes.
long gigaAvailable = sdAvailSize / 1073741824;

Got that from here: How can I check how much free space an SD card mounted on an Android device has?

Concerning your question about getting total size look here: Getting all the total and available space on Android


Edit:

Marcelo Filho has pointed out that this method is deprecated in API 18 (KitKat).

Google suggests to use getAvailableBlocksLong () and getBlockCountLong () instead. Both methods will return a long value and not a double.

Hint:

Kikiwa has pointed out that the datatype long should be used with these -now deprecated - methods, otherwise you may get negative values if the filesize is too large.

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

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();
}

How to get count of images stored in SD card

It gives the number of images present in the images folder of your SD card:

File dir = new File(Environment.getExternalStorageDirectory()
+ "/images");
File[] files = dir.listFiles();
int numberOfImages=files.length;

About the Android's Internal Storage and the external SD Card

Just to answer part of your question.From the official documention:

All Android devices have two file storage areas: "internal" and "external" storage. These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable storage medium such as a micro SD card (external storage). Some devices divide the permanent storage space into "internal" and "external" partitions, so even without a removable storage medium, there are always two storage spaces and the API behavior is the same whether the external storage is removable or not.

You do not have a removeable SDCard, but your device has a location on its internal storage that behaves like one.

Android get free size of internal/external memory

This is the way I did it :

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
}
else {
bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
}
long megAvailable = bytesAvailable / (1024 * 1024);
Log.e("","Available MB : "+megAvailable);


Related Topics



Leave a reply



Submit