Detecting if the Sd-Card had been mounted in Android

Public on February 18, 2009
Here is short article on how to detect if the sd-card had been mounted on android.

String storagestate = Environment.getExternalStorageState();
if (!storagestate.equals(Environment.MEDIA_MOUNTED) ) {
  AlertDialog alertDialog = new AlertDialog.Builder(this).create();
  alertDialog.setTitle(this.getText(R.string.error_sd_card_title));
  alertDialog.setMessage(this.getText(R.string.error_sd_card_desc));
  alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
      finish();
      return;
  } });
  alertDialog.show();
}

Quick Explanation
String storagestate = Environment.getExternalStorageState();
--- This line gets the storage state, external means your sd-card, change it to internal then it means get the state of your phone memory.

if (!storagestate.equals(Environment.MEDIA_MOUNTED) ) { }
--- This check if the storage's state is mounted, then ! at the front would reverse the result. For more values to compare to, see Environment class


Update History
   Jan 17, 2012 - Visual Update




[Advertising]Faster Thinking - Speed up your brain


Faster Thinking Game



sentiment_satisfied Emoticon