0

Hello i am working on a contact directory,i have made a custom call logs,Noe i want to display the contact details when one of the contact is clicked,I have first fetched contact_id from the contact number,and then by searching over the internet i got a function to get all details from contact id,I have put it in my code,But its not working,not giving me results,

public void getDetials(String contactID) {
        Uri myPhoneUri = Uri.withAppendedPath(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                Uri.encode(contactID));

        // Query the table
        @SuppressWarnings("deprecation")
        Cursor phoneCursor = managedQuery(myPhoneUri, null, null, null, null);

        // Get the phone numbers from the contact
        for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor
                .moveToNext()) {

            // Get a phone number
            String phoneNumber = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

            //get name
            String phonename = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));




            //get email
            String phoneemail = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));

            //get image uri..!!
            String img_uri = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            System.out
                    .println("=============my phone number ,is==================="
                            + phoneNumber + "======in call info=="+"\n =========name is===="+phonename+"=================email is========"+phoneemail);
            System.out.println("======myimgae url fopr the contact is============="+img_uri);

        }

    }
jigar jims
  • 129
  • 1
  • 12
  • I wondering whether you have forgot to pass `projection` to query. It can cause that you don't have any data back if there is no valid `projection` – rude Jul 16 '15 at 07:02
  • can you please post some code or edit to my code?thanks in advance. – jigar jims Jul 16 '15 at 07:08
  • read http://developer.android.com/reference/android/provider/ContactsContract.Data.html – pskink Jul 16 '15 at 07:10

1 Answers1

0

Here is method to read all phone numbers and emails for specified contact:

public void getContactDetails(int contactId) {
    Log.d("Details", "---");
    Log.d("Details", "Contact : " + contactId);
    final Cursor phoneCursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] {
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
            },
            Data.CONTACT_ID + "=?",
            new String[] {String.valueOf(contactId)}, null);

    try {
        final int idxAvatarUri = phoneCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Phone.PHOTO_URI);
        final int idxName = phoneCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        final int idxPhone = phoneCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Phone.NUMBER);

        while (phoneCursor.moveToNext()) {
            String phoneNumber = phoneCursor.getString(idxPhone);
            String name = phoneCursor.getString(idxName);
            String avatarUri = phoneCursor.getString(idxAvatarUri);

            Log.d("Details", "Phone number: " + phoneNumber);
            Log.d("Details", "Name: " + name);
            Log.d("Details", "Avatar URI: " + avatarUri);
        }
    } finally {
        phoneCursor.close();
    }

    final Cursor emailCursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            new String[] {
                    ContactsContract.CommonDataKinds.Email.ADDRESS,
            },
            Data.CONTACT_ID + "=?",
            new String[] {String.valueOf(contactId)}, null);

    try {
        final int idxAddress = emailCursor.getColumnIndexOrThrow(
                ContactsContract.CommonDataKinds.Email.ADDRESS);
        while (emailCursor.moveToNext()) {
            String address = emailCursor.getString(idxAddress);
            Log.d("Details", "Email: " + address);
        }
    } finally {
        emailCursor.close();
    }
}

Please note that single contact can hold multiple phone numbers and emails.

Here is how to get all contacts details:

final Cursor cur = getContentResolver().query(Data.CONTENT_URI,
        new String[]{Data.CONTACT_ID}, null, null, null);
try {
    final int idxId = cur.getColumnIndex(Data.CONTACT_ID);
    while (cur.moveToNext()) {
        final int id = cur.getInt(idxId);
        getContactDetails(id);
    }
} finally {
    cur.close();
}

Don't forget to add permission:

<uses-permission android:name="android.permission.READ_CONTACTS"/>
rude
  • 2,340
  • 2
  • 14
  • 19