How to Check How Much Free Space an Sd Card Mounted on an Android Device Has

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.

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

Why is this code incorrectly reporting free space on my Android device?

The reason for this behaviour is probably that in the case of your phone the SD card is not considered the main external storage device. The description of getExternalStorageDirectory() explains this:

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

So you're probably getting some other system that really is 0.6 GB big.

Luckily, chances are pretty high that the device you're looking for is mounted in the same parent directory as the one you're getting. So first of all, I'd check whether this is the device you're looking for with something like isExternalStorageRemovable() and if not so, try other devices in the same parent directory:

File sdStorage;
if(sdStorage.isExternalStorageRemovable()) {
sdStorage = Environment.getExternalStorageDirectory();
} else {
List<File> storages = Environment.getExternalStorageDirectory().getParentFile().listFiles();
for(File storage : storages) {
if(! storages.equals(Environment.getExternalStorageDirectory()) {
sdStorage = storage;
break;
}
}
}
if(sdStorage == null) {
// No non-removable storage device was found
}

This asumes, that there are only two devices mounted. In some rare cases, this may not be true (e.g. because a further storage device is mounted via USB-OTG); you'll have to find another workaround here. One idea may be to run the mount command which is described here via a process and work through those results to find the correct device. This however will become quite complicated.
It also asumes that all external storage devices are mounted under the same parent directory (usually /mnt/). If that isn't the case the mount command is once again your best hope.

EDIT 1: There might be some devices which mount to /sdcard without using the /mnt directory (though that might in some cases be a reference to /mnt/sdcard in which case you'll be fine). So in those cases you'll have to use the output of mount.

EDIT 2: I checked and found, that on my phone the /mnt contains quite a few directories and I only want one of those. So I came up with this solution instead of the one above:

File sdStorage;
if(sdStorage.isExternalStorageRemovable()) {
sdStorage = Environment.getExternalStorageDirectory();
} else {
Process process = null;
Scanner scan = null;
try {
process = new ProcessBuilder().command("ls", "-l", sdStorage.getParent()).redirectErrorStream(true).start();
scan = new Scanner(process.getInputStream());
while(scan.hasNextLine()) {
String line = scan.nextLine();
String[] parts = line.split("\\s");
if(parts.length > 2 && parts[2].equals("sdcard_rw")
&& ! Environment.getExternalStorageDirectory().equals(Environment.getExternalStorageDirectory().getParent() + File.seperator + parts[3]) {
sdStorage = new File(Environment.getExternalStorageDirectory().getParent() + File.seperator + parts[3]);
break;
}
} catch(IOException e) {
// Do something here
} finally {
if(scan != null)
scan.close();
if(process != null)
process.destroy();
}
}
}

The background here is, that all external storage devices seem to belong to the user sdcard_rw so this is what we can check for. Sadly the shortest method to check a files ownership within Android seems to be... Well, not very intuitive. But hopefully, this should work indepently of where exactly the various storage devices are mounted.

How to find the amount of free storage (disk space) left on Android?

Try StatFs.getAvailableBlocks. You'll need to convert the block count to KB with getBlockSize.



Related Topics



Leave a reply



Submit