Check whether the SD card is available or not programmatically
Accepted answer doesn't work for me
Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
In case if device has a built-in storage, it returns true;My solution is that to check the external files directory count, if there is more than one, device has sdcard. It works and I tested it for several devices.
public static boolean hasRealRemovableSdCard(Context context) { return ContextCompat.getExternalFilesDirs(context, null).length >= 2;}
Use Environment.getExternalStorageState()
as described in "Using the External Storage".
To get available space on external storage, use StatFs
:
// do this only *after* you have checked external storage state:File extdir = Environment.getExternalStorageDirectory();File stats = new StatFs(extdir.getAbsolutePath());int availableBytes = stats.getAvailableBlocks() * stats.getBlockSize();