1

I want to check if an SD card is inserted in an Android device (currently a Minix Neo X7).

If I use:

String state = Environment.getExternalStorageState();
if( Environment.MEDIA_MOUNTED.equals( state ) ) {  }

I get always true. And also "Environment.isExternalStorageRemovable()" returns always true.

Any idea?

timrau
  • 22,578
  • 4
  • 51
  • 64
Mat
  • 2,156
  • 2
  • 16
  • 29
  • 2
    This may be a duplicate of http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location - basically, on modern Android devices, external storage may not be an SD card (it may still be internal flash memory). – Aaron D Jan 26 '15 at 17:01
  • possible duplicate of [Check whether the SD card is available or not programmatically](http://stackoverflow.com/questions/7429228/check-whether-the-sd-card-is-available-or-not-programmatically) – AADProgramming Jan 26 '15 at 17:07
  • @Aaron: no, it's not a duplicate because I'm not interested to know the path (which is always the same for the device I'm working on), I have to know if the SD card is inserted or not – Mat Jan 27 '15 at 19:51
  • @AADTechnical: no, it's not a duplicate. As stated in my question I already tried comparing getExternalStorageState with Environment.MEDIA_MOUNTED and the result is always true – Mat Jan 27 '15 at 19:53
  • Check the mount table and see if it's mounted on the path you already know? – Aaron D Jan 28 '15 at 02:08
  • @AaronD: good point, I'll try. But there must be a way to do it with the standard API... – Mat Jan 29 '15 at 21:29

3 Answers3

3

You can check if external removable sd card is available like this

public static boolean externalMemoryAvailable(Activity context) {
File[] storages = ContextCompat.getExternalFilesDirs(context, null);
if (storages.length > 1 && storages[0] != null && storages[1] != null)
    return true;
else
    return false;

}

This works perfectly.

Jaura
  • 487
  • 5
  • 12
1

You can detect an insertable (not emulated) sdcard or a usb storage connected to device with below code.

public void CHECK_EXTERNAL_STORAGE () {
    File storage_directory = new File("/storage");
    File storage;

    for (int i = 0; i < storage_directory.listFiles().length; i++) {

        storage = storage_directory.listFiles()[i];


        if (storage.getAbsolutePath().contains("emulated")) {
            continue;
        }

        if (storage.getAbsolutePath().equals("/storage/self")) {
            continue;
        }

        if (Environment.getExternalStorageState(storage).equals(Environment.MEDIA_MOUNTED)) {

            if (Environment.isExternalStorageEmulated(storage) == false) {

                final File finalStorage = storage;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "External SD Card exists. Path: " + finalStorage.getAbsolutePath());
                        text_view2.setText("External Storage exists. Path: " + finalStorage.getAbsolutePath());
                    }
                });
            }
        }

        else {
            Log.d(TAG, "No external Storage detected.");
        }
    }
}
Burak Day
  • 907
  • 14
  • 28
0

A little long winded but...

if(Environment.MEDIA_UNMOUNTABLE.equals(false) &&  
Environment.MEDIA_UNKNOWN.equals( false ))
{
    if(Environment.MEDIA_REMOVED.equals( false) &&     
        Environment.MEDIA_UNMOUNTED.equals( false )) 
    { //mounted }
}
neutrino
  • 1
  • 1