title: android unable to get contacts from some phones
body: <p>I wrote an app that needs to read the contacts from the user's phone. Since I've put it on the market, a few users have told me that my app cannot read their contact list (it pops up an error message saying it can't get the contact list).</p>
  
  <p>For roughly 80% of the users, everything works fine, but others have this problem.</p>
  
  <p>In my MainActivity, I call the method: <code>GetContacts()</code></p>
  
  <p>If that method returns no contacts, then I call: <code>GetContacts2()</code></p>
  
  <p>If the app still can't get the contacts, then it pops up the error message. Below are the two methods I'm using. Some of the phones, this doesn't work, are Droid X2 and HTC Incredible.</p>
  
  <p>I'm also setting the required API level to 8 in my manifest, so that eliminates older phones. I also do not have log files from these users.</p>
  
  <p>NOTE: I've removed extra code that simply puts the contacts in an array for simplicity's sake.</p>
  
  <p>Any ideas on why these methods don't work on all phones?</p>
  
  <p>Any help would be greatly appreciated!!</p>
  
  <p>Mark</p>
  
  <pre><code>private void GetContacts()
  {
      try
      {
          Context context = getApplicationContext(); 
          Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
  
          while (cursor.moveToNext()) 
          { 
              contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
              contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
              hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
  
              if (Integer.parseInt(hasPhone)==1) 
              { 
                  Cursor phone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                  null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
  
                  while (phone.moveToNext())
                  {   
                      phoneNo = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      phoneType = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                  }
              } 
          }
      }
      catch (Exception e)
      {
      }
  }   
  
  private void GetContacts2()
  {
      try
      {
          ContentResolver cr = getContentResolver();
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
  
          if (cur.getCount() &gt; 0) 
          {
              while (cur.moveToNext()) 
              {
                  contactId = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                  if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) &gt; 0) 
                  {
                       if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) &gt; 0) 
                       {
                           Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{contactId}, null);
                           while (pCur.moveToNext()) 
                           {
                              phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                              phoneType = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                           } 
  
                           pCur.close();
                      }
                  }
              }
          }
      }
      catch (Exception e)
      {
      }
  }
  </code></pre>