我正在开发一个处理联系人的Android应用程序。
我在android 1.6中使用了以下代码,它运行正常。
public static Uri getProfilepicture(Activity activity, String address)
{
Uri personUri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, address);
Cursor phoneCursor = activity.getContentResolver().query(personUri,PHONE_PROJECTION, null, null, null);
if (phoneCursor.moveToFirst())
{
int indexPersonId = phoneCursor.getColumnIndex(Phones.PERSON_ID);
long personId = phoneCursor.getLong(indexPersonId);
phoneCursor.close();
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
return uri;
}
return null;
}
并在位图中获取照片,如
Bitmap bm = People.loadContactPhoto(activity,getProfilepicture(activity, ConNum, R.drawable.artist, null);
任何人都可以为Android 2.1建议代码吗?
答案 0 :(得分:1)
感谢朋友们试图帮助我。我通过以下代码解决了这个问题。
public static Bitmap getContactPhoto(Activity activity,int contactId)
{
Bitmap photo = null;
final String[] projection = new String[] {
Contacts.PHOTO_ID // the id of the column in the data table for the image
};
final Cursor contact = activity.managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projection,Contacts._ID + "=?",new String[]{String.valueOf(contactId)},null);
if(contact.moveToFirst())
{
final String photoId = contact.getString(
contact.getColumnIndex(Contacts.PHOTO_ID));
if(photoId != null)
{
photo = queryContactBitmap(activity,photoId);
}
else
{
photo = null;
}
contact.close();
}
contact.close();
return photo;
}
private static Bitmap queryContactBitmap(Activity activity,String photoId)
{
final Cursor photo = activity.managedQuery(Data.CONTENT_URI,new String[] {Photo.PHOTO},Data._ID + "=?",new String[]{photoId},null);
final Bitmap photoBitmap;
if(photo.moveToFirst())
{
byte[] photoBlob = photo.getBlob(
photo.getColumnIndex(Photo.PHOTO));
photoBitmap = BitmapFactory.decodeByteArray(
photoBlob, 0, photoBlob.length);
}
else
{
photoBitmap = null;
}
photo.close();
return photoBitmap;
}
只需传递activity对象和contactId即可。并将其存储到bitmam中。