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.");
}
}
}