Search code examples
androidemailandroid-contactsandroid-contentresolvercontactscontract

Getting null value while retrieving the CONTACT NAME from CONTACT EMAIL


I want to retrieve the contact name from the contact email, so what I have done is as follows from https://stackoverflow.com/a/18064869/5738881.

 public static String readContacts(Context context, String email) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if (!cursor.isClosed()) {
        cursor.close();
    }

    Log.e("....contact name....", email + "\n" + contactName);

    return contactName;

}

Then in onCreate(), I have coded as,

 sName = readContacts(getApplicationContext(), sEmail);

    etName.setText(sName);

But I am getting the null value. Therefore what could be the soution to fetch contact name, depending upon just the contact email address?

EDIT-1:

I have already mentioned the permission in manifest as,

<uses-permission android:name="android.permission.READ_CONTACTS"/>

EDIT-2:

As per ROHIT SHARMA's answer, I changed my code as below.

 public static String readContacts(Context context, String email) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;

    if (cursor.getCount() > 0) {
        cursor.moveToFirst();

    } else {
        return null;
    }

    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if (!cursor.isClosed()) {
        cursor.close();
    }

    Log.e("....contact name....", email + "\n" + contactName);

    return contactName;

}

But it didn't help me as well.

EDIT-3:

public static String readContacts(Context context, String email) {
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
String contactName = null;
if(cursor!=null && cursor.getCount()>0 )
{ 
 cursor.moveToFirst();
 contactName =  cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}else{ 
   return null; 
} 
if (!cursor.isClosed()) {
    cursor.close();
} 

Log.e("....contact name....", email + "\n" + contactName);

return contactName;

}

It didn't help me as well.

EDIT-4:

I tried

 public String readContacts(Context context, String email) {

    String name = null;

    // define the columns I want the query to return
    String[] projection = new String[]{
            ContactsContract.PhoneLookup.DISPLAY_NAME,
            ContactsContract.PhoneLookup._ID};

    // encode the email and build the filter URI
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));

    // query time
    Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            Log.e("....email.....", "Started uploadcontactphoto: Contact Found @ " + email);
            Log.e("....name....", "Started uploadcontactphoto: Contact name  = " + name);
        } else {
            Log.e("....email exception....", "Contact Not Found @ " + email);
        }
        cursor.close();
    }
    return name;

}

from https://stackoverflow.com/a/15007980/5738881 but didn't help as well.

Anybody got other way out?

EDIT-5:

public String readContacts() {

    ContentResolver cr = getContentResolver();

    @SuppressLint("Recycle")

    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    String id, name = null, email = null;

    if (cur != null && cur.getCount() > 0) {
        while (cur.moveToNext()) {
            id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                if (pCur != null) {
                    while (pCur.moveToNext()) {
                        String phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        System.out.println("phone" + phone);
                    }
                }
                if (pCur != null) {
                    pCur.close();
                }


                // get email and type

                Cursor emailCur = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                if (emailCur != null) {
                    while (emailCur.moveToNext()) {
                        // This would allow you get several email addresses
                        // if the email addresses were stored in an array
                        email = emailCur.getString(
                                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        String emailType = emailCur.getString(
                                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));


                        System.out.println("Email " + email + " Email Type : " + emailType);
                    }

                    if (email == sEmail) {
                        sName = name;
                    }

                }
                if (emailCur != null) {
                    emailCur.close();
                }
            }
        }
    }
    return name;
}

I tried above code instance from http://www.coderzheaven.com/2011/06/13/get-all-details-from-contacts-in-android/ and I am getting name, but it is different name than the email id owner.

So, tell me where I am going wrong..

And from above code snippet, I have also tried

if (email == sEmail) {
                            sName = name;
                        }
                        System.out.println("Email " + email + " Email Type : " + emailType);
                    }



                }
                if (emailCur != null) {
                    emailCur.close();
                }

instead of

                        System.out.println("Email " + email + " Email Type : " + emailType);
                    }

                    if (email == sEmail) {
                        sName = name;
                    }

                }
                if (emailCur != null) {
                    emailCur.close();
                }

but didn't help as well.


Solution

  • You should replace

     Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(email));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    

    with

    Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(email));
        Cursor cursor = cr.query(uri, new String[]{ContactsContract.CommonDataKinds.Email.CONTACT_ID,
                ContactsContract.Data.DISPLAY_NAME}, null, null, null);
    

    and then test it..