通过photo_ID显示联系人的照片

时间:2012-10-23 07:13:34

标签: android image contacts photo

我让用户在我的应用程序中选择一个联系人,然后在主屏幕小部件上显示它,但是没有显示照片,我不知道出了什么问题。

这就是我获取照片的参考资料:

...
Cursor c = null;
try {
    c = getContentResolver().query(uri, new String[] {
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.PHOTO_ID },
            null, null, null);

    if (c != null && c.moveToFirst()) {
        String number = c.getString(0);
        int type = c.getInt(1);
        String name = c.getString(2);
        int photo = c.getInt(3);
        showSelectedNumber(type, number, name, photo);
    }
}

这就是我展示它的方式:

public void showSelectedNumber(int type, String number, String name, int photo) {
    mAppWidgetPrefix.setText(name);
    pickedNumber.setText(number);
    pickedPhoto.setImageResource(photo);
}

为什么不起作用?

1 个答案:

答案 0 :(得分:11)

您正尝试将ContactsContract.Data表格中的行ID作为资源ID设置到ImageView。当然它不会起作用。它甚至没有任何意义。

您应首先从数据库中检索原始照片,然后才能显示它。

例如,您可以使用此代码在指向图像数据的行ID的帮助下检索图像位图(我已经重新创建了一些代码来测试它):

private void queryContactInfo(int rawContactId) {
    Cursor c = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] {
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_ID
            }, ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[] { Integer.toString(rawContactId) }, null);
    if (c != null) {
        if (c.moveToFirst()) {
            String number = c.getString(0);
            int type = c.getInt(1);
            String name = c.getString(2);
            int photoId = c.getInt(3);
            Bitmap bitmap = queryContactImage(photoId);
            showSelectedNumber(type, number, name, bitmap);
        }
        c.close();
    }
}

private Bitmap queryContactImage(int imageDataRow) {
    Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {
        ContactsContract.CommonDataKinds.Photo.PHOTO
    }, ContactsContract.Data._ID + "=?", new String[] {
        Integer.toString(imageDataRow)
    }, null);
    byte[] imageBytes = null;
    if (c != null) {
        if (c.moveToFirst()) {
            imageBytes = c.getBlob(0);
        }
        c.close();
    }

    if (imageBytes != null) {
        return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); 
    } else {
        return null;
    }
}

public void showSelectedNumber(int type, String number, String name, Bitmap bitmap) {
    mInfoView.setText(type + " " + number + " " + name);
    mImageView.setImageBitmap(bitmap); // null-safe
}

您还可以看到http://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html 作为获取联系人照片的便捷提供者目录。还有一个例子。

相关问题