Search code examples
androidandroid-contentproviderandroid-contactsandroid-cursor

Android Contacts Content Provider sometimes returns empty phone numbers


Here is the code used to get the contact information :

String id = data.getData().getLastPathSegment();
 Cursor cursor = getActivity().getContentResolver()
                  .query(ContactsContract.Data.CONTENT_URI,
                         new String[] {ContactsContract.Data.DISPLAY_NAME},
                                    ContactsContract.Data.CONTACT_ID + "=?",
                                    new String[]{id},
                                    null);

// short circuit if we didn't pick a contact
     if (cursor.getCount() == 0) {
      return;
     }

     String contact = "";
     String contactName = "";

//code to get contact name
     if (cursor.moveToFirst() && cursor.getString(0) != null) {      
          contact = contact + cursor.getString(0) + ",";
          contactName = cursor.getString(0);
          }
     else {
            contact = contact + ","; //changed
          }
     cursor.close();

 // code to get phone number
   cursor = getActivity().getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                
        newString[{ContactsContract.CommonDataKinds.Phone.NUMBER, 
            ContactsContract.CommonDataKinds.Phone.TYPE},
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
            new String[]{id},null);

      if (cursor.moveToFirst() && cursor.getString(0) != null) {
           contact = contact + cursor.getString(0) + ",";
           contact = contact + cursor.getString(0) + ",";
       } 
      else {
             contact = contact + ",,"; //changed
           }
      cursor.close();

 //Code to get email
    cursor = getActivity().getContentResolver()
            .query(
             ContactsContract.CommonDataKinds.Email.CONTENT_URI,
             new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS},
             ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
             new String[]{id},null);

     if (cursor.moveToFirst() && cursor.getString(0) != null) {         
          contact = contact + cursor.getString(0);
     }
     cursor.close();

   contact = contact.replace("+", "");
   Log.i("TAG", "CONTACT INFO = " + contact);

What I am trying to do:

Here, I query the contact name, phone number and email from Contacts through content provider and concatenate all three into a single string, with commas separating them within the string .

The problem :

I get the contact name and email, without any issues. However, when I select some contacts from contact, the phone number returned is empty.

The Contacts that return empty phone numbers are phone numbers from a different country, from my current location.

I am right now in Canada and when I select Canadian Contacts, I get the phone number correctly.

However, when I select phone numbers from India, the number is not returned, instead, I get empty result.

The same happens when I select a Contact with numbers like "12345" or "5555555555"etc.

My Question

Could it be that Android is trying to validate the authenticity of the number and returning an empty number, if it finds the phone number Invalid? *(probably not! Must be my mistake!)*

How can I solve this issue? Can't I get the value of the phone number just like how it is saved in the Contacts by the user?

I apologize for any unambiguity in the code or my question. I am a novice programmer and thanks a lot for all your inputs!


Solution

  • One thing i observed from that code is, you are reading contacts with out using

    ContactsContract.Contacts.CONTENT_URI
    

    I Modified your code and created a new method to print all contacts.

    public static void fetchContacts(Context context) {
    
        String phoneNumber = null;
        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
        StringBuffer output = new StringBuffer();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
    
        if (cursor.getCount() > 0) {
    
            while (cursor.moveToNext()) {
    
                String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
                String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
                long hasPhoneNumber = Long.parseLong(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
    
                if (hasPhoneNumber > 0) {
                    output.append("\n Name:" + name);
                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] { contact_id }, null);
    
                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    output.append("\n Number:" + phoneNumber);
                    System.out.println("Number:::::"+phoneNumber);
                    System.out.println("Contact:::::"+output.toString());
                }
                    phoneCursor.close();
                }
            }
        }else{
    //          Toast.makeText(context, "No contacts Found", Toast.LENGTH_LONG).show();
            }
    
        }