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) は以前のバージョンでも機能しますが、3 番目のパラメーターがないとサムネイルのみを取得するようです。

ドキュメントには次のように書かれています:

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

しかし、このメモの背後にあるリンクは、現在のページに再びつながっているようです

画像の uri を取得できましたが、その後はどうでしょうか。

4

2 に答える 2

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 に答える