Yanshof Yanshof - 1 year ago 69
Android Question

why i can't get all the contacts on my phone ?

i wrote a code that return all the contacts that are on my android phone.
but when i run it on my android phone i see that even if i have 600 contacts - i get on my code only 173 contacts

How to get all the contacts ?
Why i get only 173 of them ?

The code:

private void CollectAllContacts(Activity activity){
ArrayList<ContactData> contactDataList = new ArrayList<ContactData>();

try {

ContentResolver cr = activity.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
ContactData newContact = new ContactData();

String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


catch(Exception e)
// write to log.

Answer Source

I was facing same problem. I resolved it by using intent. You can try this

 Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
            pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
            startActivityForResult(pickContactIntent, 100);

then onActivityResult You can handle it

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100 && resultCode == getActivity().RESULT_OK && null != data) {
        Uri uri = data.getData();
        String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,

        Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);
        int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        do {
            String phoneNumber = people.getString(indexNumber);
            phoneNumber = phoneNumber.replaceAll("[-]", "").replaceAll(" ", "");

        } while (people.moveToNext());

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download