Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the SDCard is present, boolean is always true

In my splash screen, I want to check if the phone has a SDCard. The Boolean statement is beneath:

    Boolean isSDPresent = android.os.Environment.getExternalStorageState()
            .equals(android.os.Environment.MEDIA_MOUNTED );

So, if I have the SDCard in the slot on my phone, this boolean will return true, so far so good. When I go to the "Unmount SDCard" from the settings menu, and removes the SDCard, then kill the app and launching it again, the boolean will also be true..

And if I launches the Astro File Manager after unmounting and removing the sdcard, I can still access the /mnt/sdcard path, why?

How can I manage to accomplish this?

Thanks in advance!

EDIT

Testing with the following code:

File path = Environment.getExternalStorageDirectory(); 
String pathS = path.getPath();

When the SDCard is in the slot, the pathS contains mnt/sdcard, but when I removes the SDCard the pathS is still /mnt/sdcard ...

like image 949
Tobias Moe Thorstensen Avatar asked Aug 23 '12 08:08

Tobias Moe Thorstensen


2 Answers

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.

like image 112
brianestey Avatar answered Nov 08 '22 14:11

brianestey


Shouldn't it be:

boolean isPresent = Environment.getExternalStorageState().equals(
   Environment.MEDIA_MOUNTED
);

As the documentation states for Environment.getExternalStorageState():

Gets the current state of the primary "external" storage device.

And for the Environment.MEDIA_MOUNTED-constant:

getExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access.

And this will work from API Level 1+

like image 2
Lukas Knuth Avatar answered Nov 08 '22 12:11

Lukas Knuth