Wednesday, February 18, 2009

Detecting if the Sd-Card had been mounted in Android

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.setButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
  } });;

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
Post a Comment