Varundroid
Varundroid

Reputation: 9234

Android - Unable to fetch contact photo?

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

Answers (3)

user1292458
user1292458

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

Femi
Femi

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

Sunil Kumar Sahoo
Sunil Kumar Sahoo

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

http://thinkandroid.wordpress.com/2010/01/19/retrieving-contact-information-name-number-and-profile-picture/

Thanks Deepak

Upvotes: 2

Related Questions