Android - 无法获取联系人照片?

时间:2011-06-01 13:22:53

标签: android image fetch contact

我正在尝试使用联系人ID获取联系人图片。

这是我的代码: -

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);
}

此代码为我的所有联系人返回null,包括那些有图片的联系人。

我在这里做错了什么?

请帮助!!

感谢。

3 个答案:

答案 0 :(得分:4)

您的联系人是否已从Facebook同步?因为那些似乎无法访问。

如果情况并非如此,您可以尝试这样做:

InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(this.getContentResolver(), uri);

不确定您是否有ContactsContract的导入。

答案 1 :(得分:2)

您可以使用以下代码加载联系人的照片。

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);

否则 您可以看到以下网址

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

由于 迪帕克

答案 2 :(得分:0)

您可以使用此代码获取android联系人数据库表中的照片: 首先获取联系人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); 
    }
相关问题