在 Android 上检索联系人图像

Posted

技术标签:

【中文标题】在 Android 上检索联系人图像【英文标题】:Retrieve contact image on Android 【发布时间】:2014-09-15 05:41:32 【问题描述】:

我目前正在开发一个 android 项目,我正在尝试在设备中查找联系人电话号码并检索联系人信息,例如联系人姓名和联系人图片。获取合同名称工作正常,但是在尝试获取照片 uri 时会抛出空指针。

下面是我正在使用的代码:

public ContactInformation getContactInfoFromPhoneNumber(String number)
    
        ContactInformation contactInformation = new ContactInformation();
        if (number == null)
        
            return null;
        
        number = number.replace(" ", "");

        if (number.startsWith("+"))
        
            number = number.substring(3);
        

        ContentResolver contentResolver = context.getContentResolver();

        Uri uri = ContactsContract.Data.CONTENT_URI;
        String[] projection = new String[] ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME;
        String selection = "REPLACE (" + ContactsContract.CommonDataKinds.Phone.NUMBER + ", \" \" , \"\" ) LIKE ?";
        String[] selectionArgs =  "%" + number ;
        Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, null);

        if (cursor.moveToFirst())
        
            contactInformation.contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
            contactInformation.photoUri = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            cursor.close();

            return contactInformation;
        
        else
        
            cursor.close();
            return null;
        
    

更新

以下是我根据 Itzik Samara 的回答更新的代码。

以下是我如何查找联系人:

public ContactInformation getContactInfoFromPhoneNumber(String number)
    
        ContactInformation contactInformation = new ContactInformation();
        if (number == null)
        
            return null;
        
        number = number.replace(" ", "");

        if (number.startsWith("+"))
        
            number = number.substring(3);
        

        ContentResolver contentResolver = context.getContentResolver();

        Uri uri = ContactsContract.Data.CONTENT_URI;
        String[] projection = new String[] ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME;
        String selection = "REPLACE (" + ContactsContract.CommonDataKinds.Phone.NUMBER + ", \" \" , \"\" ) LIKE ?";
        String[] selectionArgs =  "%" + number ;
        Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, null);

        if (cursor.moveToFirst())
        
            contactInformation.contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
            contactInformation.photoBase64String = getContactPhoto(cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)));
            cursor.close();

            return contactInformation;
        
        else
        
            cursor.close();
            return null;
        
    

下面是我的 getContactPhoto 函数:

private String getContactPhoto(int contactID)
    
        Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactID);
        Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
        Cursor cursor = context.getContentResolver().query(photoUri, new String[]ContactsContract.Contacts.Photo.PHOTO,
                null, null, null);
        if (cursor == null)
        
            return null;
        
        if (cursor.getCount() > 0)
        
            while (cursor.moveToNext()) 
                byte[] data = cursor.getBlob(0);
                String base64String = Base64.encodeToString(data, Base64.DEFAULT);
                cursor.close();
                return base64String;
            
        
        return null;
    

if 语句失败并直接跳出 if 以返回 null,就好像游标中没有任何内容一样。

【问题讨论】:

【参考方案1】:

这个功能对我有用:

private byte[] getContactPhoto(Context context,long contact_id) 

    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contact_id);
    Uri photoUri = Uri.withAppendedPath(contactUri,ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    Cursor cursor = context.getContentResolver().query(photoUri, new String[]ContactsContract.Contacts.Photo.PHOTO, null, null, null);
    if(cursor == null)
        return null;
    try 
        if(cursor.getCount() > 0) 
            if (cursor.moveToFirst()) 
                byte[] data = cursor.getBlob(0);
                cursor.close();
                if (data != null)
                    return data;
            
        

    
    finally 
        cursor.close();
    
    return null;





   private void getContacts(Context context) 

    ContentResolver cr = context.getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
    try 
        if (cursor.getCount() > 0) 

            while (cursor.moveToNext()) 

                long contact_id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                Contact friend = new Contact();
                String name= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String email = getContactEmail(context, contact_id);
                if(!name.contains("@") && !email.matches("")) 
                    friend.setName(name);
                    friend.setEmail(email);
                    friend.setImage(getContactPhoto(context, contact_id));
                    friend.setPhone(getContactMobilePhoneNumber(context, contact_id));

                    mContacts.add(friend);
                
            

        
    
    finally 
        cursor.close();
    




【讨论】:

这需要任何特殊权限吗? contact_id 指的是什么 权限: 谢谢仍然没有用,请检查我修改后的问题

以上是关于在 Android 上检索联系人图像的主要内容,如果未能解决你的问题,请参考以下文章

Android - 检索联系人照片和显示

检索系统默认 Android 联系人图片

在 Android 3.0 上检索电话联系人

从 Android 联系人中检索电话号码在三星上不起作用

Android 3.0 - 如何通过 ContactsContract 检索所有联系人

获取选定的联系人图片 - Android