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

前端 未结 2 523
谎友^
谎友^ 2021-01-27 18:08

I need the contact images of my contacts as bitmaps.

I found this code:

        Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTE         


        
2条回答
  •  隐瞒了意图╮
    2021-01-27 18:18

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

提交回复
热议问题