Get High-res Contact Photo As Bitmap Below Api Level 14 Android
I need the contact images of my contacts as bitmaps. I found this code: Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id))
Solution 1:
It's simple: there is not high res contact picture below API 14. You need to use the low res on lower APIs or set min-sdk to 14.
It was added in API 14 and lower APIs will never have anything behind the Uri you created by hand.
Solution 2:
use this:
ContentResolvercr= getContentResolver();
Cursorcur= cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
Stringid= cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
intSDK_INT= android.os.Build.VERSION.SDK_INT;
if(SDK_INT>=11){
image_uri=cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (image_uri != null) {
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),Uri.parse(image_uri));}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
bitmap=loadContactPhoto(cr, Long.valueOf(id));
if(bitmap!=null)
{
//show bitmap
}
}
}
}
used methods:
public Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uriuri= ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStreaminput= ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
returnnull;
}
return BitmapFactory.decodeStream(input);
}
Post a Comment for "Get High-res Contact Photo As Bitmap Below Api Level 14 Android"