在Android上检索联系人图像

在Android上检索联系人图像,android,android-contacts,Android,Android Contacts,我目前正在从事一个android项目,我试图在设备中查找联系人电话号码,并检索联系人姓名和联系人图像等联系人信息。获取合同名称工作正常,但是,在尝试获取照片uri时会抛出空指针 下面是我正在使用的代码: public ContactInformation getContactInfoFromPhoneNumber(String number) { ContactInformation contactInformation = new ContactInformation(

我目前正在从事一个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;
        }
    }
谢谢你能提供的帮助

更新

下面是我根据伊兹克·萨马拉的答案更新的代码

下面是我如何进行联系人查找的

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,就好像游标中没有任何内容一样

此功能适用于我:

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



}

这需要任何特殊权限吗?联系方式是什么?权限:谢谢,似乎仍然不起作用,请检查我的问题