Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether the SD card is available or not programmatically

My app is working for mobiles which have an SD card only. So programmatically I want to check if the SD card is available or not and how to find the SD card free space. Is it possible?

If yes, how do I do it?

like image 483
naresh Avatar asked Sep 15 '11 10:09

naresh


People also ask

Why SD card is not available?

Possible Reasons for SD Card Not Showing Up on Android Don't worry, the “SD card not detected on Android” problem might be caused by the following reasons: Wrong format or SD card reading errors. SD card is not compatible with the device. Wrong data transfer mode on Android.

How do I know if my SD card is default storage?

Go to your storage settings. Navigate to Settings and then Storage. Here you should find three storage options: RAM, internal storage, and memory card. The memory card refers to your newly connected SD card.


2 Answers

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable();  if(isSDSupportedDevice && isSDPresent) {   // yes SD-card is present } else {  // Sorry } 
like image 181
Paresh Mayani Avatar answered Oct 05 '22 23:10

Paresh Mayani


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; } 
like image 29
Jemo Mgebrishvili Avatar answered Oct 05 '22 23:10

Jemo Mgebrishvili