Reputation: 9234
i am trying to fetch a contact image using contact id.
Here is my code :-
public Bitmap getDisplayPhoto(Long id)
{
Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI,id);
InputStream input = Contacts.openContactPhotoInputStream(this.getContentResolver(), uri);
if (input == null)
{
return null;
}
return BitmapFactory.decodeStream(input);
}
This code is returning null for all of my contacts including those which has an image.
What am i doing wrong here?
Please Help!!
Thanks.
Upvotes: 1
Views: 2021
Reputation:
You can used this code to fetch the photo in android contact database table: First of fetch the contact id.
long _id = Long.parseLong(id);
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, _id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if(input!=null){
flag=true;
Bitmap bitmap=BitmapFactory.decodeStream(input);
ImageView image=(ImageView)findViewById(R.id.imageView1);
image.setImageBitmap(bitmap);
}
Upvotes: 0
Reputation: 64700
Are your contacts synced from Facebook? Because those appear to not be accessible.
If that's not the case, you may want to try this:
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(this.getContentResolver(), uri);
Wasn't sure if you had the import for ContactsContract
in place.
Upvotes: 4
Reputation: 53657
You can use the following code to load photo of a contact.
Cursor c = getContentResolver().query(People.CONTENT_URI, new String[] { People._ID, People.NAME }, null, null, null);
int idCol = c.getColumnIndex(People._ID);
long id = c.getLong(idCol);
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
Bitmap bitmap = People.loadContactPhoto(context, uri, R.drawable.icon, null);
Otherwise You can see the following URL
Thanks Deepak
Upvotes: 2