如何通过android 2.3.6中的联系人提供程序获取联系人照片? [英] How do you get a contact photo through the contact provider in android 2.3.6?

查看:75
本文介绍了如何通过android 2.3.6中的联系人提供程序获取联系人照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this version that works for android 4 (String email is a gmail address):

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的信息吗?因为它没有...)

What is its 2.3.6 version? (Also shouldn't the android SDK warn me about using API 11 when I set API 8 as my minimal API support? Because it didn't...)

推荐答案

您无需使用ContactsContract.CommonDataKinds.Email.PHOTO_URI就可以获取联系人照片uri,

You can get the contact photo uri without using ContactsContract.CommonDataKinds.Email.PHOTO_URI this way:

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)方法以这种方式获取照片流:

Alternatively, you can get the photo stream utilizing method ContactsContract.Contacts .openContactPhotoInputStream(ContentResolver, Uri) this way:

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

这篇关于如何通过android 2.3.6中的联系人提供程序获取联系人照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆