4

我有这个适用于 android 4 的版本(String email是一个 gmail 地址):

private Uri getPhotoUriFromEmail(String email) {
    Uri u = null;
    String[] projection = { ContactsContract.CommonDataKinds.Email.PHOTO_URI };
    String photoUri;
    ContentResolver cr = context.getContentResolver();
    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?", 
            new String[]{email}, null);
    if (emailCur.moveToNext()) { 
        photoUri = emailCur.getString(
                emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.PHOTO_URI));
        u = Uri.parse(photoUri);
    }
    return u;
}

它的 2.3.6 版本是什么?(当我将 API 8 设置为我的最小 API 支持时,android SDK 也不应该警告我使用 API 11 吗?因为它没有......)

4

1 回答 1

5

ContactsContract.CommonDataKinds.Email.PHOTO_URI 您可以在不使用这种方式的情况下获取联系人照片 uri :

private Uri getPhotoUriFromEmail(String email) {
    Uri u = null;
    String[] projection = { ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String photoUri;
    ContentResolver cr = getContentResolver();
    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
            projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
            new String[]{email}, null);
    if (emailCur.moveToNext()) {
        int columnIndex = emailCur.getColumnIndex(
                              ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
        long contactId = emailCur.getLong(columnIndex);

        u = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, 
                                       contactId);
        u = Uri.withAppendedPath(u, 
                                ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }
    return u;
}

或者,您可以通过以下方式获取照片流ContactsContract.Contacts .openContactPhotoInputStream(ContentResolver, Uri)

private InputStream getPhotoInputStreamFromEmail(String email) {
    Uri u = null;
    String[] projection = { ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String photoUri;
    ContentResolver cr = getContentResolver();
    Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
            new String[]{email}, null);
    if (emailCur.moveToNext()) {
        int columnIndex = emailCur.getColumnIndex(
                ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
        long contactId = emailCur.getLong(columnIndex);

        u = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
                contactId);
        return ContactsContract.Contacts.openContactPhotoInputStream(cr, u);
    }
    return null;
}
于 2012-12-31T15:21:49.240 回答