Android: Finding If Sd-card Is Present
Solution 1:
There doesn't seem to be any public API for this currently. You could check by looking at /proc/mounts
but your code would then be device-dependent, since not all devices mount secondary external storage (SD card) at the same place.
Solution 2:
I think /storage/sdcard0/
is used for internal sdcard and /storage/sdcard1/
is used for external sd card if there are two storage option present.
if you are checking a file is present either in any of sdcard or not you should check all possible paths.
String path;
if(newFile("/mnt/sdcard/yourpath").exists()) {
path="/storage/sdcard/yourpath";
} elseif(newFile("/storage/sdcard0/yourpath").exists()) {
path="/storage/sdcard0/yourpath";
} elseif(newFile("/storage/sdcard1/yourpath").exists()) {
path="/storage/sdcard1/yourpath";
}
Solution 3:
I've created a check of my own, and it works. It checks if secondary, real SD card is present. Tested it on Samsung Galaxy s5 neo, Alcatel one touch 5020x, and on HTC One X. The code should work on KITKAT devices since it uses the app default dir to check.
I create a String of default app path storage on primary storage. Then change "primary" to "secondary", then try to create a folder and check for existance.
Heres the code:
StringprimaryStorage= Environment.getExternalStorageDirectory().getAbsolutePath();
StringsecondaryStorage= System.getenv("SECONDARY_STORAGE");
BooleanhasSecondary=false;
StringinternalSD= getExternalFilesDir(null) + "/test";
StringexternalSD= internalSD.replace(primaryStorage, secondaryStorage);
try{
Filedir=newFile(externalSD);
dir.mkdirs();
if (dir.isDirectory()) {
dir.delete();
hasSecondary = true;
}
} catch (Exception e) {
}
Solution 4:
I try like this
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
Log.i("tag", "SDCard is mounted");
}
or this example would help you
Post a Comment for "Android: Finding If Sd-card Is Present"