Check If the Sdcard Is Present, Boolean Is Always True

Check if the SDCard is present, boolean is always true

I've found that phones, like the Galaxy phones from Samsung, have /mnt/sdcard point to internal memory and not the external SD card as expected.

You can know if the path returned by Environment.getExternalStorageDirectory() is actually the external SD card with a call to Environment.isExternalStorageRemovable()

Just wanted to add from the docs for getExternalStorageDirectory() this important note:

Note: don't be confused by the word "external" here. This directory
can better be thought as media/shared storage. It is a filesystem that
can hold a relatively large amount of data and that is shared across
all applications (does not enforce permissions). Traditionally this is
an SD card, but it may also be implemented as built-in storage in a
device that is distinct from the protected internal storage and can be
mounted as a filesystem on a computer.

Check whether the SD card is available or not programmatically

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
}

A sure shot method to find if SDCard is present

You should use getExternalFilesDirs method. This method returns path(s) to both internal memory & Micro SD card (if present). The second returned path would be path of Micro SD card. But that path may not be root directory of Micro SD card.

getExternalFilesDirs introduced in API level 19 is the only official method that can be used to get SD card path. All others methods will return either public or private storage locations(paths) of internal memory.

I have tested this method on my phone & emulator. Kindly, note that I have passed null as parameter for getExternalFilesDirs during testing. But You could also pass parameter(s) like Environment.DIRECTORY_PICTURES, Environment.DIRECTORY_MOVIES etc.

TEST RESULTS:-

When my phone has memory card inserted, getExternalFilesDirs returned :

1st path (internal memory): /storage/emulated/0/Android/data/my_package.appname/files

2nd path (Micro SD): /storage/sdcard1/Android/data/my_package.appname/files

Note: root directory of my SD card is: /storage/sdcard1

On my emulator, getExternalFilesDirs returned :

1st path (internal memory): /storage/emulated/0/Android/data/my_package.appname/files

2nd path (Micro SD): /storage/11E9-4214/Android/data/my_package.appname/files

Note: On emulator, SD card is emulated(not real one).

If Micro SD card is ejected (either on phone or emulator) getExternalFilesDirs returns null

1st path (internal memory): path to internal memory - It varies depending on device

2nd path (Micro SD): null

BUT ;

On some devices, you may not get the Micro SD card path. It means getExternalFilesDirs will return only one path(internal memory). Second path would be empty i.e. it will not return any second path.

Reason: OEM of that particular device may not have set the path (i.e. SECONDARY_STORAGE environment variable) in the device.

This answer for this question says,

the OEM must have set the SECONDARY_STORAGE environment variable in
the device specific init.rc file as mentioned here:
https://source.android.com/devices/storage/config-example.html

Also the comment made by somesh on the above mentioned answer says,

If that environment variable is not set, you can't write to the sdcard
by making use of getExternalFilesDirs since its not going to return
the sdcard path.

In those kind of cases, assume that memory card is not present and save whatever you want to save in internal memory.

To Summarize, If getExternalFilesDirs doesn't return second path or returned second path is null, don't store in SD card (store in internal storage); else store in second path returned by getExternalFilesDirs

Suggestion:

To understand about different storage locations in Android, please go through my answer on stackoverflow

Android check for SD Card mounted always returns true

External storage is not the same as SD card, at least not on all devices. Devices that have internal flash memory (for example my Nexus S does) threat this as " external storage".

Now, devices that have both internal flash and SD card, threat internal flash as external memory and SD card is then added as directory under this external memory.

From programmers view it's a pain, but not much we can do about it.



Related Topics



Leave a reply



Submit