1

我有联系人的查找键,现在我想使用这些查找键将联系人图像作为位图/输入流获取。Android 文档有助于获取图像,但联系人 ID 不是查找键。

public InputStream openPhoto(long contactId) {
 Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
 Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
 Cursor cursor = getContentResolver().query(photoUri,
      new String[] {Contacts.Photo.PHOTO}, null, null, null);
 if (cursor == null) {
     return null;
 }
 try {
     if (cursor.moveToFirst()) {
         byte[] data = cursor.getBlob(0);
         if (data != null) {
             return new ByteArrayInputStream(data);
         }
     }
 } finally {
     cursor.close();
 }
 return null;
}

从柱子跑到柱子,但没有帮助。谢谢

编辑日期 04-09-2012

按照@Sreejith Krishnan R 的建议进行了尝试,但在 logcat 中收到以下异常

09-04 03:16:26.359: E/AndroidRuntime(24008): java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/lookup/0r272-382C544E2C562C382C4E582C42.2649i11.1987r6285-382C544E2C562C382C4E582C42.1987r6440-382C544E2C562C382C4E582C42.2829r6475-382C544E2C562C382C4E582C42/photo, calling user: com.xyz, calling package:com.xyz
09-04 03:16:26.359: E/AndroidRuntime(24008):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
09-04 03:16:26.359: E/AndroidRuntime(24008):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
4

1 回答 1

3

[更新] 现在这可以从 API 级别 5 开始使用

public Bitmap getAvatar(Context context, String lookupKey){
    Uri uri = getDataUri(context, lookupKey);
    if (uri == null){
        return null;
    }
    Cursor  cursor = context.getContentResolver().query(
            uri,
            new String[] {ContactsContract.Data.DATA15},
            null,
            null,
            null
    );
    if (cursor == null){
        return null;
    }
    try{
        if (cursor.moveToFirst()){
            byte [] bytes = cursor.getBlob(0);
            InputStream inputStream = new ByteArrayInputStream(bytes);
            return BitmapFactory.decodeStream(inputStream);
        }
    } finally {
        cursor.close();
    }
    return null;
}

public Uri getDataUri(Context context, String lookupKey){
    Cursor cursor = context.getContentResolver().query(
            Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey),
            new String[] {ContactsContract.Contacts.PHOTO_ID},
            null,
            null,
            null
    );
    if (cursor == null){
        return null;
    }
    try {
        if (cursor.moveToFirst()){
            long id = cursor.getLong(0);

            /**http://developer.android.com/reference/android/provider/ContactsContract.ContactsColumns.html#PHOTO_ID
             * If PHOTO_ID is null, consult PHOTO_URI or PHOTO_THUMBNAIL_URI,
             * which is a more generic mechanism for referencing the contact photo,
             * especially for contacts returned by non-local directories (see ContactsContract.Directory).
             */

            if (id == 0){
                if (Build.VERSION.SDK_INT < 11){
                    return null;
                }
                return getPhotoThumbnailUri(context, lookupKey);
            }
            return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id);
        }
    } finally {
        cursor.close();
    }
    return null;
}

//Available only for API level 11+
public Uri getPhotoThumbnailUri(Context context, String lookupKey){
    Cursor cursor = context.getContentResolver().query(
            Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey),
            new String[] {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI},
            null,
            null,
            null
    );
    if (cursor == null){
        return null;
    }
    try{
        if (cursor.moveToFirst()){
            return Uri.parse(cursor.getString(0));
        }
    } finally {
        cursor.close();
    }
    return null;
}
于 2012-05-23T07:49:18.857 回答