Reputation: 1129
I am using the following Cursor to retrieve contacts that are in the phone only? However, the code gives me contacts in both SIM and phone. Is it possible to get contacts in the Phone ONLY, with their photo (if available) ,phone number and display name?
cursor = getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
int contactIdIdx = cursor.getColumnIndex(Phone._ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
cursor.moveToFirst();
do {
try {
Contact c = new Contact();
String idContact = cursor.getString(contactIdIdx);
String name = cursor.getString(nameIdx);
c.setName(name);
Log.i(TAG,storage.getDriveName()+": "+name);
String phoneNumber = cursor.getString(phoneNumberIdx);
Log.i(TAG,storage.getDriveName()+": "+phoneNumber);
c.setPhoneNumber(phoneNumber);
if(phoneNumber != null)
c.setPhotoUri(getPhotoUri(Long.valueOf(fetchContactIdFromPhoneNumber(phoneNumber))));
contacts.add(cbd);
} catch(Exception e) {
Log.e(TAG,storage.getDriveName()+": "+e.getMessage());
}
} while (cursor.moveToNext());
} catch (Exception e) {
Log.e(TAG,storage.getDriveName()+": "+e.getMessage());
}
if (cursor != null) {
cursor.close();
}
}
Upvotes: 0
Views: 336
Reputation: 1501
Uri uri = ContactsContract.Data.CONTENT_URI;
final String[] PROJECTION = new String[]{
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_URI,
};
Cursor cursor = getContentResolver().query(uri, PROJECTION, ContactsContract.Data.MIMETYPE + " = ?",
new String[]{
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
}, ContactsContract.Contacts.DISPLAY_NAME + " ASC ", null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
String photo = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.PHOTO_URI));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
String phoneNumber = null;
@SuppressLint("Recycle") Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
String email = null;
@SuppressLint("Recycle") Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contactId}, null);
while (emailCur.moveToNext()) {
email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
contactList.add(new ContactModal(contactId, name, email, photo, phoneNumber));
}
contactList is Modal class
Upvotes: 0
Reputation: 3784
i have implemented this code below is worked for me check this out:
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// filters contact with phone numbers
contactPickerIntent
.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(contactPickerIntent, CONTACT_PICKER);
now in the activityresult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
AndyUtills.removeSimpleProgressDialog();
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER:
Cursor cursor = null;
Bitmap bitmap = null;
Uri result = data.getData();
// String id = result.getLastPathSegment();
cursor = getContentResolver().query(result, null, null, null,
null);
// String contactId=cursor.get;
if (cursor == null) {
AndyUtills.showToast("Selection Fail", this);
return;
}
if (!cursor.moveToFirst()) {
cursor.close();
AndyUtills.showToast("Selection Fail", this);
return;
}
String contactName = cursor
.getString(cursor
.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
.toString().trim()));
String contactNumber = cursor
.getString(cursor
.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER)));
// check for contact whether it is already inserted or not
if (checkForSameNumber(contactNumber)) {
AndyUtills.showToast("Same contact is already inserted",
this);
return;
}
String photo = null;
// get photo uri from contacts
try {
photo = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.PHOTO_URI));
Log.i("TAG", "Activity Result: Old:" + photo);
} catch (Exception e) {
e.printStackTrace();
}
cursor.close();
break;
}
} else {
// gracefully handle failure
Log.w("Auto Respond", "Warning: activity result not ok");
}
}
hope you can get all the thing that you want.
Upvotes: 0