【问题标题】:Why can't I get the URI of profile pic from a contact number?为什么我无法从联系人号码中获取个人资料图片的 URI?
【发布时间】:2012-05-09 10:40:06
【问题描述】:

我有一个自定义来电屏幕,可以当前显示来电的姓名和电话号码。如果联系人有个人资料图片集(在您自己的手机上),则该图像应显示在我创建的 ImageView 中。任何不在手机上的号码都会显示 Android 图标。

测试代码后,无论电话号码是否存储在手机(模拟器)上,我都会得到Android图标。以下是检索来电者联系人照片的代码(如果有的话):

public Uri getPhotoUri(long contactId) { //contactId takes the phone number.

    ContentResolver contentResolver = getContentResolver();

    try {
        Cursor cursor = contentResolver
            .query(ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.CONTACT_ID
                    + "="
                    + contactId
                    + " AND "

                    + ContactsContract.Data.MIMETYPE
                    + "='"
                    + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                    + "'", null, null);

        if (cursor != null) {
        if (!cursor.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, contactId);
    return Uri.withAppendedPath(person,
        ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
  }

该方法调用上述方法,并将图片分配给ImageView。问题是else语句总是被执行:

IncomingCallListener.getId(getID)));是用于获取相应头像的实际电话号码。

public void showContactPhoto(){

    Uri u = getPhotoUri(IncomingCallListener.getId(getID()));
    if (u != null) {    
        qcbContactPic.setImageURI(u);
        Log.d("PHOTO", "ID launched");
    } else {
        qcbContactPic.setImageResource(R.drawable.ic_launcher);
        Log.d("PHOTO", "Default launched");
    }
}

有没有人知道为什么“u”仍然为空?

注意:联系人图片存储在媒体库中。

【问题讨论】:

  • 您在模拟器中将图片添加到联系人了吗?
  • 您的目标 SDK 级别是多少? ICS 还是姜饼?在 ICS (lvl 14) 中,您有 PHOTO_FILE_ID 可用于查找高分辨率图像,否则您几乎无法使用存储在 PHOTO 中的缩略图。
  • @blackcrow 获取名称和号码工作正常,但问题是此代码正在注册“u”为空,因此仅显示“ic_launcher”,这应该是用于联系人的未存储,或尚未为联系人设置个人资料图片。

标签: android imageview uri photo contact


【解决方案1】:

试试这个

Uri uri = ContentUris.withAppendedId(
                    ContactsContract.Contacts.CONTENT_URI, longId);
            ContentResolver cr = getContentResolver();
            InputStream input = ContactsContract.Contacts
                    .openContactPhotoInputStream(cr, uri);
            if (input == null) {
                return null;
            } else
                return BitmapFactory.decodeStream(input);

使用 ImageView 的 setImageBitmap() 方法。

【讨论】:

  • 我可以向你保证,这是有效的代码。尝试找出问题的确切位置。
  • 这是假设设置图像:qcbContactPic.setImageBitmap(getShowCPhoto(IncomingCallListener.getId(getID())));
  • 会不会是我使用的图片是jpeg而不是位图?
  • 我在上面的评论中使用了 setImageBitmap()。 qcbContactPic 是我的 ImageView。从那里,没有图像出现。
猜你喜欢
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
相关资源
最近更新 更多