如何从联系号码获取个人资料图片的URI?

时间:2017-02-11 09:34:48

标签: java android uri photo contact

我创建了自定义来电屏幕,可以显示来电的姓名和电话号码。如果联系人具有配置文件图片集,则该图像应显示在ImageView中。我调用下面的方法,并将图片分配给ImageView。问题是else语句总是执行:

public static Uri getPhotoUri(String savedNumber, Context context) {
    try {
        Cursor cur = context
                .getContentResolver()
                .query(
                        ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + getContactIDFromNumber(savedNumber,
                                context)
                                + " AND "
                                + ContactsContract.Data.MIMETYPE
                                + "='"
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                + "'", null, null);
        if (cur != null) {
            if (!cur.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
            getContactIDFromNumber(savedNumber, context));
    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

public static int getContactIDFromNumber(String contactNumber,Context context) {
    int phoneContactID = 0;
    Cursor contactLookupCursor = context.getContentResolver().query(
            Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(contactNumber)),
            new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID},
            null, null, null);

    try {
        contactLookupCursor.moveToFirst();
        while (contactLookupCursor.moveToNext()) {
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor
                    .getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        }
    } finally {
        contactLookupCursor.close();
    }
    return phoneContactID;
}

IncomingActivity:

@Override
protected void onResume() {
    super.onResume();

    callerNumber.setText(savedNumber);
    callerName.setText(CommonMethods.getCallerName(savedNumber, this));

    callerImageUri = CommonMethods.getPhotoUri(savedNumber, this);
    if (callerImageUri != null) {
        callerImage.setImageURI(callerImageUri);
    } else {
        callerImage.setImageResource(R.drawable.contact_default);
    }
}

有没有人知道为什么" callerInamgeUri"保持无效?

0 个答案:

没有答案