-2

I used this to check if there is an external SD card before:

public static boolean externalMemoryAvailable() {
    return android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED);
}

but it returns true and the path of the external SD card is "/storage/emulated/0 while my test machine(Mi Note Pro) does not have external SD card.So I wonder if there are other function to check the external SD card. Hope someone help me!Thanks!

DYZ
  • 51,549
  • 10
  • 60
  • 87
lixuan deng
  • 288
  • 3
  • 13

3 Answers3

2
public boolean externalMemoryAvailable() {
    if (Environment.isExternalStorageRemovable()) {
      //device support sd card. We need to check sd card availability.
      String state = Environment.getExternalStorageState();
      return state.equals(Environment.MEDIA_MOUNTED) || state.equals(
          Environment.MEDIA_MOUNTED_READ_ONLY);
    } else {
      //device not support sd card. 
      return false;
    }
  }

This question has already been asked on Stack Overflow and has an answer here

Community
  • 1
  • 1
Descoladan
  • 43
  • 1
  • 2
  • 6
0

Hello you are on the right track..

just use

Environment.isExternalStorageRemovable()

to chek if it is actual sdcard or not. In morden devices internal storage is also consider as sdcard.

AJay
  • 1,183
  • 9
  • 18
  • you are right,but this function can't work in some devices cause it returns false all the time – lixuan deng Jan 18 '17 at 08:31
  • @lixuandeng yeah that may be issue, but you can first check `ndroid.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED);` this and than check if it `isExternalStorageRemovable()` – AJay Jan 18 '17 at 09:38
  • you mean this? ` public static boolean externalMemoryAvailable() { if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) { if (Environment.isExternalStorageRemovable()) return true; else return false; } else return false; }` – lixuan deng Jan 19 '17 at 01:52
  • yes you got that. – AJay Jan 19 '17 at 05:59
0
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(isSDPresent)
{
  // yes SD-card is present
}
else
{
 // Sorry
}

This question was solved at Check whether the SD card is available or not programmatically

Community
  • 1
  • 1
Truong Hieu
  • 3,309
  • 2
  • 20
  • 37