Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly detect if SD card is available

Tags:

android

I use the following code to check if the SD card is available but on a Samsung Galaxy II and Note devices that run Android 4.x and later the OS simulate having a SD card even if in reality there is no SD card on the device.

private boolean isSDCardAvailable() {
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

If SD card is available I then call getExternalCacheDir() get the directory else I display erro r to the user and call getCacheDir() to use internal storage.

The method above return TRUE and ALL devices even if they don't have a SD card.

like image 996
Lennie Avatar asked Nov 17 '25 21:11

Lennie


1 Answers

see this method is available since API level 11, which let you know if Internal Memory working as External storage. in some devices getExternalStorageDirectory() returns true even if SD-Card is not available. read below docs for details.

public static boolean isExternalStorageEmulated ()

Added in API level 11

Returns whether the device has an external storage device which is emulated. If true, the device does not have real external storage, and the directory returned by getExternalStorageDirectory() will be allocated using a portion of the internal storage system.

Certain system services, such as the package manager, use this to determine where to install an application.

Emulated external storage may also be encrypted - see setStorageEncryption(android.content.ComponentName, boolean) for additional details.

Android docs

like image 128
AAnkit Avatar answered Nov 19 '25 11:11

AAnkit