I'm working on an android application thats dealing with contact.
I have used the following code in android 1.6 and it works fine.
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;
}
and getting photo in bitmap like
Bitmap bm = People.loadContactPhoto(activity,getProfilepicture(activity, ConNum, R.drawable.artist, null);
Can any on开发者_开发百科e suggest code for android 2.1 please?
Thanks Friends to trying to help me. i have solve the problem by following code.
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;
}
in that just pass the activity object and contactId. and store it into bitmam.
精彩评论