javaandroidandroid-contactscontactscontract

Get some contact info data from phone


I need to get list of id, first name, last name, number(or numbers), email, website of an android device contacts. I know by getting id I can query about phone numbers this is not a big deal. But I don't know how I should make query to get all these columns correctly.

I supposed for names I need ContactsContract.CommonDataKinds.StructuredName for email I need ContactsContract.CommonDataKinds.Email.ADDRESS ,for id Contacts and for website ContactsContract.CommonDataKinds.Website.URL.

My code returns strange values like a single digit number for GIVEN_NAME, null for FAMILY_NAME, one of contact numbers for Email.ADDRESS.

However I think the problem is query URI, which one should I use?

ContentResolver cr = getActivity().getContentResolver();

String[] projection = new String[] {
                    Contacts._ID,
                  ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
  ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                    ContactsContract.CommonDataKinds.Website.URL,
                    ContactsContract.CommonDataKinds.Email.ADDRESS};

Cursor nameCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    projection,
                    null,
                    null,
                    null);
while (nameCur.moveToNext()) {
   String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
   String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
   String email = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
   Integer id= nameCur.getInt(nameCur.getColumnIndex(Contacts._ID));
   String website = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
//do some work with strings...                
}

Solution

  • here is my solution: first we need a query for getting all contact id in the table of "raw_contacts"

    List<Integer> ret = new ArrayList<Integer>();
    ContentResolver contentResolver = getActivity().getContentResolver();
    // Row contacts content uri( access raw_contacts table. ).
    Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI;
    // Return _id column in contacts raw_contacts table.
    String queryColumnArr[] = {ContactsContract.RawContacts._ID};
    // Query raw_contacts table and return raw_contacts table _id.
    Cursor cursor = contentResolver.query(rawContactUri, queryColumnArr, null, null, null);
    

    then we query table "data" for extra information for each contact id:

    // Data content uri (access data table. )
    Uri dataContentUri = ContactsContract.Data.CONTENT_URI;
    // Build query columns name array.
    List<String> queryColumnList = new ArrayList<String>();
    // ContactsContract.Data.CONTACT_ID = "contact_id";
    queryColumnList.add(ContactsContract.Data.CONTACT_ID);
    // ContactsContract.Data.MIMETYPE = "mimetype";
    queryColumnList.add(ContactsContract.Data.MIMETYPE);
    queryColumnList.add(ContactsContract.Data.DATA1);
    queryColumnList.add(ContactsContract.Data.DATA2);
    queryColumnList.add(ContactsContract.Data.DATA3);
    queryColumnList.add(ContactsContract.Data.DATA4);
    queryColumnList.add(ContactsContract.Data.DATA5);
    queryColumnList.add(ContactsContract.Data.DATA6);
    queryColumnList.add(ContactsContract.Data.DATA7);
    queryColumnList.add(ContactsContract.Data.DATA8);
    queryColumnList.add(ContactsContract.Data.DATA9);
    queryColumnList.add(ContactsContract.Data.DATA10);
    queryColumnList.add(ContactsContract.Data.DATA11);
    queryColumnList.add(ContactsContract.Data.DATA12);
    queryColumnList.add(ContactsContract.Data.DATA13);
    queryColumnList.add(ContactsContract.Data.DATA14);
    queryColumnList.add(ContactsContract.Data.DATA15);
    // Translate column name list to array.
    String queryColumnArr[] = queryColumnList.toArray(new String[queryColumnList.size()]);
    // Build query condition string. Query rows by contact id.
    StringBuffer whereClauseBuf = new StringBuffer();
    whereClauseBuf.append(ContactsContract.Data.RAW_CONTACT_ID);
    whereClauseBuf.append("=");
    whereClauseBuf.append(rawContactId);
    // Query data table and return related contact data.
    Cursor cursor = contentResolver.query(dataContentUri, queryColumnArr, whereClauseBuf.toString(), null, null);
    

    last cursor contains all the data of any contact and for get that data we need switch case:

    String mimeType =cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
    switch (mimeType) {
                // Get email data.
                case ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE:
                    // Email.ADDRESS == data1
                    String emailAddress = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                    int emailType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                    data.setDataType(emailType);
                    data.setDataValue(emailAddress);
                    ret1.add(data);
                    con.setEmailList(ret1);
                    break;
                // Get organization data.
                case ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE:
                    // Organization.COMPANY == data1
                    String company = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
                 con.setCompany(company);
                    break;
                // Get phone number.
                case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
                    // Phone.NUMBER == data1
                    String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // Phone.TYPE == data2
                    int phoneTypeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    data.setDataType(phoneTypeInt);
                    data.setDataValue(phoneNumber);
                    ret1.add(data);
                    con.addPhoneList(ret1);
                    break;
                // Get display name.
                case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE:
                    // StructuredName.DISPLAY_NAME == data1
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
                    // StructuredName.GIVEN_NAME == data2
                    String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    // StructuredName.FAMILY_NAME == data3
                    String familyName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                    ret.add("Display Name : " + displayName);
                    ret.add("Given Name : " + givenName);
                    ret.add("Family Name : " + familyName);           
                    break;
                // Get website.
                case ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE:
                    // Website.URL == data1
                    String websiteUrl = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
                    // Website.TYPE == data2
                    int websiteTypeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE));
                    String websiteTypeStr = getEmailTypeString(websiteTypeInt);
                    ret.add("Website Url : " + websiteUrl);
                    ret.add("Website Type Integer : " + websiteTypeInt);
                    ret.add("Website Type String : " + websiteTypeStr);
                    break;
            }
    

    this is not my compelete code just for getting idea. hope this will help someone!