我正在工作的安卓app.In我的应用程序,我必须显示联系人显示名称和显示图片。为了通过号码获取联系人displayname,我使用了以下代码。
public String getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = " ";
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}它的工作很好,.Please帮助我得到这样的联系显示图片。
发布于 2012-08-16 11:50:02
下面的代码是为我工作的。
public Drawable getContactDisplaypicByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String contactId = null;
InputStream input = null;
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
Drawable d=null;
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri,projection, null, null, null);
if (contactLookup.moveToFirst()) {
contactId = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.PhoneLookup._ID));
Uri uri1 = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri1);
}
else {
Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found @ " + number);
return d; // contact not found
}
if (input == null) {
Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name);
d=getResources().getDrawable(R.drawable.ic_launcher);
return d; // no photo
} else {
Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name);
d=Drawable.createFromStream(input, "");
System.out.println("ddddddddddddddddddddddddddddddddd"+d);
return d;
}
}https://stackoverflow.com/questions/11950405
复制相似问题