này was very helpful, đây là mã cuối cùng của tôi để lấy của người gọi tên, id, và Ảnh:
private void uploadContactPhoto(Context context, String number) {
Log.v("ffnet", "Started uploadcontactphoto...");
String name = null;
String contactId = null;
InputStream input = null;
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
if (cursor.moveToFirst()) {
// Get values from contacts database:
contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
// Get photo of contactId as input stream:
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri);
Log.v("ffnet", "Started uploadcontactphoto: Contact Found @ " + number);
Log.v("ffnet", "Started uploadcontactphoto: Contact name = " + name);
Log.v("ffnet", "Started uploadcontactphoto: Contact id = " + contactId);
} else {
Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found @ " + number);
return; // contact not found
}
// Only continue if we found a valid contact photo:
if (input == null) {
Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name);
return; // no photo
} else {
this.type = contactId;
Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name);
}
... sau đó chỉ cần làm bất cứ điều gì bạn muốn với "đầu vào" (ảnh của họ dưới dạng InputStream), "na tôi ", và" contactId ".
Và đây là các tài liệu liệt kê các ~ 15 hoặc lâu hơn cột mà bạn có thể sử dụng, chỉ cần thêm chúng vào các dự báo gần đầu của mã lên trên: http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html
Đối với cơ sở của những người khác, tôi đã viết một bài có chứa toàn bộ mã để truy vấn tên, hình ảnh, ID liên lạc, vv với lời giải thích khá. Mã có chứa các đoạn mã như được tìm thấy trên các câu trả lời khác nhau, nhưng được tổ chức và thử nghiệm hơn. Liên kết: http://hellafun.weebly.com/home/get-information-of-a-contact-from-number – Usman