I need to read the contacts from device. The fields I required are ID
, Display Name
, Phone Number
(All types) Email id
(All types) and 'photo'. For this right now I am doing like this.
1 : First I am reading all the id
s from ContactsContract.Contacts.CONTENT_URI;
as shown below
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
String[] projection = {ID};
Cursor contactsCursor = mContentResolver.query(contactsUri, projection, null, null,
"upper(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
2 : Then iterating through this cursor to read requred fields of all contacts.
if (contactsCursor.moveToFirst()) {
do {
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex(ID));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone, work email etc corresponding to each contact
String[] columns = {CONTACT_ID, PHOTO_URI, DISPLAY_NAME, MIME_TYPE, DATA_1, DATA_2, DATA_4};
String selection = ContactsContract.Data.CONTACT_ID + "=" + contactId;
Cursor dataCursor = mContentResolver.query(dataUri, columns,
selection,
null, null);
if (dataCursor.moveToFirst()) {
// Getting Display Name
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
do {
// Getting Phone numbers
String mimeType = dataCursor.getString(dataCursor.getColumnIndex(MIME_TYPE));
switch (mimeType) {
case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE:
String phoneNumber = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
switch (dataCursor.getInt(dataCursor.getColumnIndex(DATA_2))) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
homePhone = phoneNumber;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
mobilePhone = phoneNumber;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
workPhone = phoneNumber;
break;
default:
otherPhone = phoneNumber;
}
break;
// Getting emails
case ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE:
switch (dataCursor.getInt(dataCursor.getColumnIndex(DATA_2))) {
case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
homeEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
workEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
break;
default:
otherEmail = dataCursor.getString(dataCursor.getColumnIndex(DATA_1));
}
break;
// getting photo Uri
case ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE:
if (dataCursor.getString(dataCursor.getColumnIndex(PHOTO_URI)) != null) {
photoUri = Uri.parse(dataCursor.getString(dataCursor.getColumnIndex(PHOTO_URI)));
}
break;
}
} while (dataCursor.moveToNext());
} while (contactsCursor.moveToNext());
The queries work fine but the problem is it is taking too much time to iterate and get details of each contact. First query is returning quickly , but the whole delay is now in the second part, that is iterating through each row of the first query and querying for each fields. Can this be done in a single join query so that I can optimize the performance?