Get high-res contact photo as bitmap below API level 14 Android

纵然是瞬间 提交于 2019-12-31 05:31:11

问题


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

which works pretty well, but the function openContactPhotoInputStream(cr, my_contact_Uri, true) is only available on API 14+. openContactPhotoInputStream(cr, my_contact_Uri) works also on earlier versions, but without the 3rd parameter it seems to retrieve only the thumbnail.

in the documentation it says:

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

but the link behind this note seems to lead on the current page again

I could get the uri of the image, but what then?


回答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.




回答2:


use this:

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

used methods:

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


来源:https://stackoverflow.com/questions/18671814/get-high-res-contact-photo-as-bitmap-below-api-level-14-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!