public class MainActivity extends AppCompatActivity {
public static final String TAG = "Main_Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: " + getAvailableSpaceInKB());
Log.d(TAG, "onCreate: " + getAvailableSpaceInMB());
Log.d(TAG, "onCreate: " + getAvailableSpaceInGB());
}
/**
* @return Number of kilo bytes available on External storage
*/
public static long getAvailableSpaceInKB() {
final long SIZE_KB = 1024L;
long availableSpace;
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
return availableSpace / SIZE_KB;
}
/**
* @return Number of Mega bytes available on External storage
*/
public static long getAvailableSpaceInMB() {
final long SIZE_KB = 1024L;
final long SIZE_MB = SIZE_KB * SIZE_KB;
long availableSpace;
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
return availableSpace / SIZE_MB;
}
/**
* @return Number of gega bytes available on External storage
*/
public static long getAvailableSpaceInGB() {
final long SIZE_KB = 1024L;
final long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;
long availableSpace;
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
return availableSpace / SIZE_GB;
}
}
Copyright 2021 M. Fadli Zein