title: How to get all contacts from phonebook & SIM card in Android?
body: <p>I am working to get all the contacts from phone book and SIM card in my application. I want to store those all contacts in my application SQLite DB. The code with which I am working is working fine in normal conditions. Getting problem in below conditions:</p>
  
  <ol>
  <li>with a contact without name, that is only number.</li>
  <li>Contacts from SIM card.</li>
  </ol>
  
  <p>These 2 types of contacts, are not provided to me by my code. I am using the below code:</p>
  
  <pre><code>public void getDefaultContactsToDB(){
          CallBackDatabase callbackDB = new CallBackDatabase(RequestCallBack.this);
          callbackDB.open();
  
          //clean the database before entering new values.
          callbackDB.deleteTable(CallBackDatabase.DATABASE_TABLE);
  
          ContentResolver cr = getContentResolver();
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
          if (cur.getCount() &gt; 0) {
              while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  Cursor cur1 = cr.query( 
                          ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                          ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                          new String[]{id}, null); 
                  while (cur1.moveToNext()) { 
                      //to get the contact names
                      ArrayList&lt;String&gt; numbers= new ArrayList&lt;String&gt;();
                      String contactNoumber="";
                      String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                      if(name!=null){
                          Log.e("Name :", name);
                          if(name.equalsIgnoreCase("011999999999999999")){
                              System.out.println("got it");
                          }
                      }
                      //to get the contact email
                      String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                      if(email!=null)
                          Log.e("Email", email);
  
                      String hasNoumber = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                      if(Integer.parseInt(hasNoumber)&gt;0){ 
                          Cursor pCur = getContentResolver().query(
                                  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "
                                          +cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)),null, null);
                          int i = 0;
                          while (pCur.moveToNext()) {
                              //to get the contact number
                              contactNoumber = pCur.getString(pCur.getColumnIndex("DATA1"));
                              if(contactNoumber.equalsIgnoreCase("011999999999999999")){
                                  System.out.println("got it");
                              }
                              contactNoumber = Constant.removeSpecialCharacters(contactNoumber);
                              Log.e("contactNoumber", contactNoumber);
                              // Getting Phone numbers
                              String numType = null;
                              if(pCur.getString(pCur.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
  
                                  switch(pCur.getInt(pCur.getColumnIndex("data2"))){
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
                                      numType = "HOME";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
                                      numType = "MOBILE";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
                                      numType = "WORK";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER :
                                      numType = "OTHER";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT :
                                      numType ="OTHER";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK :
                                      numType = "CALLBACK";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_CAR :
                                      numType ="CAR";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN :
                                      numType = "COMPANY MAIN";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME :
                                      numType = "FAX HOME";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK :
                                      numType = "FAX WORK";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN :
                                      numType = "MAIN";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN :
                                      numType = "ISDN";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_MMS :
                                      numType = "MMS";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX :
                                      numType = "OTHER FAX";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER :
                                      numType = "PAGER";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO :
                                      numType = "RADIO";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX :
                                      numType ="TELEX";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD :
                                      numType = "TTY TDD";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE :
                                      numType = "WORK MOBILE";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER :
                                      numType = "WORK PAGER";
                                      break;
                                  case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM :
                                      numType = "CUSTOM";
                                      break;
                                  default:
                                      break;
                                  }
                              }
  
                              numbers.add(i, contactNoumber+"("+numType+")");
  
                              i++;
                          }
                          String numInDB = null;
                          for (int j = 0; j &lt; i; j++) {
                              if(j==0)
                                  numInDB =numbers.get(j);
                              else
                                  numInDB =numInDB + "," +numbers.get(j);
                          }
                          if(contactNoumber.length()&gt;0){
                              if(name!=null){
  
                              }else{
                                  name = contactNoumber;
                              }
                              callbackDB.InsertContacts(null, name+"="+numInDB, contactNoumber, email);
                          }
                      }
                  } 
                  cur1.close();
              }
              //CLOSE DB
              callbackDB.close();
          }
      }
  </code></pre>