2

I can't get a working Cursor containing all contact's phone numbers:

I obtain lookupUri using this method:

public static Uri getLookupUri (ContentResolver mContentResolver, String number) {
    Uri uri=null;
    Cursor contactLookupCursor =  
            mContentResolver.query(
                    Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                            Uri.encode(number)), 
                            new String[] {PhoneLookup._ID, PhoneLookup.LOOKUP_KEY}, 
                            null,
                            null, 
                            null);

    if (contactLookupCursor.moveToNext()) {
         uri=Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_LOOKUP_URI, contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.LOOKUP_KEY)));
    }

    contactLookupCursor.close();
    return uri;
}

Then I would like to get a Cursor (phones) containing all contact's phone numbers:

    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER};

    Cursor phones = localContentResolver.query(lookupUri, projection, null, null, null);

but I only get IllegalArgumentException: Invalid column data1

Jonik
  • 80,077
  • 70
  • 264
  • 372
stepic
  • 673
  • 9
  • 19

3 Answers3

0

To get phone number using contact name use this code

 ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        "DISPLAY_NAME = '" + NAME + "'", null, null);
    if (cursor.moveToFirst()) {
        String contactId    =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        //
        //  Get all phone numbers.
        //
        Cursor phones = cr.query(Phone.CONTENT_URI, null,
            Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_HOME:
                    // do something with the Home number here...
                    break;
                case Phone.TYPE_MOBILE:
                    // do something with the Mobile number here...
                    break;
                case Phone.TYPE_WORK:
                    // do something with the Work number here...
                    break;
            }
        }
        phones.close();
    }
    cursor.close();

I tested and it is work i get it from this answer How to get contacts' phone number in Android

Community
  • 1
  • 1
3bdoelnaggar
  • 1,109
  • 8
  • 18
0

You're trying to query the phone number from a Contacts row. Contacts rows don't have phone numbers. The value of ContactsContract.CommonDataKinds.Phone.NUMBER is "data1", and there is no "data1" column in the ContactsContract.Contacts table.

To find all the phone numbers for a Contact, do this:

Find the contact you want. Your getLookupUri is fine for doing this, except you should probably call withAppendedPath() with CONTENT_VCARD_URI. See the Javadoc.

To get all the phone numbers for the Contact, use the Contacts.Entity table.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
0

The following code will fetch all the contacts in an android device.

public class GetContactFromLookup extends Activity {
    int CONTACTS_REQUEST_CODE =1;
    static ContentResolver cr;
    Activity thisActivity;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.test);
        thisActivity = this;
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, 1);


            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          switch (requestCode) {
          case 1 :
            if (resultCode == Activity.RESULT_OK) {

            Uri uri = data.getData();
            List<String> items = uri.getPathSegments();
            String lookup_key = items.get(2);
            Log.v("", "id-->"+items.get(2));

            Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ;//People.CONTENT_URI;
            Cursor mqCur =  managedQuery(myContacts, null, null, null, null);
            if(mqCur.moveToFirst())
            {              
                do
                {                  
                    String contact_id = mqCur.getString(mqCur
                            .getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));
                    if(lookup_key.equals(contact_id)){  
                        String phone_no = mqCur.getString(mqCur
                                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          Log.v("", "phone_no-->"+phone_no);
                    }                   
                }
                while(mqCur.moveToNext());
            }

            }           
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}

I think this will help you.

Satheeshkumar
  • 452
  • 4
  • 13
  • 1
    I don't want to fetch all contacts, but to retrieve all phone numbers of a specific contact having already a lookupUri – stepic Apr 03 '12 at 09:48
  • Ok then see this [link](http://stackoverflow.com/questions/8594987/obtaining-phone-number-from-lookup-uri) . This will help you. – Satheeshkumar Apr 03 '12 at 11:53
  • That example only extracts contactId from lookupUri. I was trying to get rid of contactId because, like Android Reference says: _Let's say your app stored a long ID of a contact. Then the user goes and manually joins the contact with some other contact. Now there is a single contact where there used to be two, and the stored long contact ID points nowhere. The lookup key helps resolve the contact in this case._ – stepic Apr 03 '12 at 14:53