Wednesday, September 23, 2015

// //

Detect SD Card present or not in android programmatically

This is the simple code on how to detect the sd card if present or not using code.


This is the MainActivity.java

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  TextView state = (TextView) findViewById(R.id.sdcardstatus);
  if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
   state.setText("SD card is present");
  } else {
   state.setText("SD card is not present");
  }
 }
}




The activity_main.xml



    





HAPPY CODING!