0

我需要我的联系人的联系人图像作为位图。

我找到了这段代码:

        Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id));
        InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(cr, my_contact_Uri, true);
        BufferedInputStream buf = new BufferedInputStream(photo_stream);
        Bitmap my_btmp = BitmapFactory.decodeStream(buf);
        buf.close();
        return my_btmp;

效果很好,但是函数 openContactPhotoInputStream(cr, my_contact_Uri, true) 仅在 API 14+ 上可用。openContactPhotoInputStream(cr, my_contact_Uri) 也适用于早期版本,但没有第三个参数,它似乎只检索缩略图。

文档中它说:

See Also
if instead of the thumbnail the high-res picture is preferred

但是此注释后面的链接似乎再次指向当前页面

我可以得到图像的 uri,但那又如何呢?

4

2 回答 2

3

很简单:API 14以下没有高清联系人图片。需要在较低的API上使用低分辨率或将min-sdk设置为14。

它是在 API 14 中添加的,更低的 API 将永远不会在您手动创建的 Uri 后面有任何内容。

于 2013-09-07T09:23:51.127 回答
0

用这个:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
         int SDK_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
           }
        }
  }
}

使用的方法:

public Bitmap loadContactPhoto(ContentResolver cr, long id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {
         return null;
    }
    return BitmapFactory.decodeStream(input);
}
于 2015-06-27T11:45:28.953 回答