-1

I've found a very good method to get paths to all the sd-cards that device has,not only Environment.getExternalStorageDirectory() , but also external ones. This method is secrond at How can I get external SD card path for Android 4.0+?

So, my question is how can I find out free available space on sd card knowing it's path, for example, "storage/extSdCard" or any other

in other words i need a method :

public int getExternalAvailableSpaceInBytes(String sdRootPath) {
//here's this method using this sdRootPath
}
Community
  • 1
  • 1
Vlad Alexeev
  • 2,072
  • 6
  • 29
  • 59

1 Answers1

-1
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
@SuppressWarnings("deprecation") 
long sdAvailSize = (long)stat.getAvailableBlocksLong() * (long)stat.getBlockSizeLong(); 
}
else{
@SuppressWarnings("deprecation")
double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize();
}

double gigaAvailable = sdAvailSize / 1073741824;    //1073741824 =1GB
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77