通过photo_ID显示联系人的照片 [英] Displaying contact's photo by photo_ID

查看:96
本文介绍了通过photo_ID显示联系人的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让用户在我的应用程序中选择一个联系人,并将其显示在主屏幕小部件上,但未显示照片,也不知道出了什么问题.

I let the user select a contact in my app, and I display it on the home screen widget, but the photo is not displayed and I don't know what's wrong.

这是我对照片的引用:

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

为什么不起作用?

推荐答案

您正在尝试将 ContactsContract.Data 表中的行ID设置为 ImageView 中的资源ID代码>.当然可以,这是行不通的.甚至没有任何意义.

You are trying to set ID of the row from ContactsContract.Data table as a resource ID into your ImageView. And surely it wouldn't work. It doesn't even make any sense.

您应该首先从数据库中检索原始照片,然后才可以显示它.

You should retrieve the original photo from database first and only then you can show it.

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

For example, you can use this code to retrieve image bitmap with the help of row ID pointing to the image data (i've recreated some pieces of code just to test it):

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 作为获取联系人照片的便捷提供者目录.还有一个例子.

You can also see http://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html as a convenient provider directory for getting contacts' photos. There is an example as well.

这篇关于通过photo_ID显示联系人的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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