Android Get Free Size of Internal/External Memory

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

Get free space on internal memory

this post might fit well to your question.

also check this thread. there is so much info here on SO.

googled a bit and here is the solution (found at android git)

File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);

Get Internal and External Memory size in MarshMallow

In lolipop or marshmallo you must use

Environment.getExternalStorageDirectory().getAbsolutePath();

to get external storage path instead of System.getenv("SECONDARY_STORAGE");

Refer beolw code for get external and internal storage in >android 5.0

public static String getTotalInternalMemorySize() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return formatSize(totalBlocks * blockSize);
}

public static String getAvailableExternalMemorySize() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return formatSize(availableBlocks * blockSize);
}

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.

How to Check available space on android device ? on SD card?

Try this code:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());

long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;

System.out.println("Megs: " + megAvailable);

Update:

getBlockCount() - return size of SD card;

getAvailableBlocks() - return the number of blocks that are still accessible to normal programs (thanks Joe)



Related Topics



Leave a reply



Submit