Get Uri from a contact having the email

0

Hi guys and girls (of course)

How do I get the email from a contact stored in my phonebook to get the Uri from this? and then from there I get the rest of this information. This last one I have already implemented. I saw an example in link to do it with the number and get the photo , but I do not know what to do to do it from the email

Greetings

    
asked by San Juan 29.06.2017 в 21:05
source

2 answers

2

This is what I did

Cursor cursor= activity.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null,ContactsContract.Data.DATA1 + " = '"+mail+"' ", null,null);
String contID = null;
if (cursor !=null && cursor.moveToFirst()){
    contID = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID));
    cursor.close();
}

After having the contact id with the I make another query and I get all the data of this

    
answered by 21.09.2017 / 02:02
source
1

I just created that function, to get the contact id from the email.

public static String getContactByEmail(Context context, String email) {
    if (email == null) return null;

    Uri CONTACTS_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String[] PROJECTION = new String[]{
            ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID,
            ContactsContract.Data.DATA1
    };
    final String SELECTION = ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?";
    String[] SELECTION_ARG = new String[]{email};

    Cursor c = context.getContentResolver().query(CONTACTS_URI, null, SELECTION, SELECTION_ARG, null);
    String contactID = null;
    if (c != null && c.moveToFirst()) {
        contactID = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID));
        c.close();
        return contactID;
    } else return null;

}

Your use

String idContact = getContactByEmail(this,"[email protected]");

    
answered by 21.09.2017 в 15:25