0

アンドロイド 2.3.3

例の1つで次のコードを見つけました...

String image = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

                if (image != null) {
                    contactViewHolder.imgContact.setImageURI(Uri.parse(image));
                } else {
                    contactViewHolder.imgContact
                            .setImageResource(R.drawable.addcontactsmall2);
                }

しかし、アプリケーションを実行しようとすると、IllegalStateException が発生します。ただし、他の例では、この URI はどこにも見つかりませんでした。これは連絡先の写真を取得する正しい方法ですか?

これは、カーソルを PHOTO_URI ::: の上に置いたときに表示されるものです。

文字列 android.provider.ContactsContract.ContactsColumns.PHOTO_URI = "photo_uri"

    public static final String PHOTO_URI 
    Added in API level 11 
    A URI that can be used to retrieve the contact's full-size photo. 
If PHOTO_FILE_ID is not null, this will be populated with a URI based off CONTENT_URI. 
Otherwise, this will be populated with the same value as PHOTO_THUMBNAIL_URI. 
A photo can be referred to either by a URI (this field) or by ID (see PHOTO_ID).
If either PHOTO_FILE_ID or PHOTO_ID is not null, PHOTO_URI and PHOTO_THUMBNAIL_URI shall not be null (but not necessarily vice versa). 
Thus using PHOTO_URI is a more robust method of retrieving contact photos. 

    Type: TEXT


    Constant Value: "photo_uri" 
4

1 に答える 1

4

これがお役に立てば幸いです (連絡先は getId() で識別されます):

/**
 * @return the photo URI
 */
public Uri getPhotoUri() {
    try {
        Cursor cur = this.ctx.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND "
                        + ContactsContract.Data.MIMETYPE + "='"
                        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
                null);
        if (cur != null) {
            if (!cur.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
            .parseLong(getId()));
    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

使用法は次のとおりです。

Uri u = objItem.getPhotoUri();
if (u != null) {
        mPhotoView.setImageURI(u);
} else {
        mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}
于 2013-04-07T04:20:04.317 に答える