如何获得Android的联系缩略图 [英] How to get Android Contact thumbnail

查看:302
本文介绍了如何获得Android的联系缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图适配器,我想在 NewView的方法如下:

  @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){        最后LayoutInflater吹气= LayoutInflater.from(背景);
        视图V = inflater.inflate(布局,父母,假);        长的ContactID = Long.valueOf(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
        字符串CONTACTNAME = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        布尔hasPhone = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
        串thumbnailUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));        TextView的name_text =(TextView中)v.findViewById(R.id.name_entry);
        如果(name_text!= NULL){
            name_text.setText(联系人姓名);
        }        name_text.setTag(新接触(的ContactID,hasPhone));        ImageView的缩略图=(ImageView的)v.findViewById(R.id.thumbnail);
        如果(thumbnailUri!= NULL){
            thumbnail.setImageURI(Uri.parse(thumbnailUri));
        }其他{
            thumbnail.setImageResource(R.drawable.ic_launcher);
        }        返回伏;
    }

但是当我尝试解析乌里存储在thumbnailUri,我收到以下错误:

 一月八日至九日:58:38.619:我/的System.out(1471):resolveUri没有坏的位图网址:内容://com.android.contacts/contacts/1 /照片

我要对这个错误的方式?任何帮助将大大AP preciated!


解决方案

 私人乌里getPhotoUriFromID(字符串ID){
    尝试{
        光标CUR = getContentResolver()
                .query(ContactsContract.Data.CONTENT_URI,
                        空值,
                        ContactsContract.Data.CONTACT_ID
                                +=
                                + ID
                                +和
                                + ContactsContract.Data.MIMETYPE
                                +='
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                +',NULL,NULL);
        如果(CUR!= NULL){
            如果(!cur.moveToFirst()){
                返回null; //没有照片
            }
        }其他{
            返回null; //错误光标过程
        }
    }赶上(例外五){
        e.printStackTrace();
        返回null;
    }
    乌里人= ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI,的Long.parseLong(ID));
    返回Uri.withAppendedPath(人,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

这就是你需要通过接触ID功能,你会得到你可以在ImageView的轻松设置图像的URI。

使用像imageView.setImageURI(URI)这个函数的响应URI

希望这会为你的code工作。

I have a listview adapter and I'm trying the following in the newView method:

@Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        long contactId = Long.valueOf(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))); 
        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        boolean hasPhone = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
        String thumbnailUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)); 

        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(contactName);
        }

        name_text.setTag(new Contact(contactId, hasPhone));

        ImageView thumbnail = (ImageView) v.findViewById(R.id.thumbnail);
        if (thumbnailUri != null) {
            thumbnail.setImageURI(Uri.parse(thumbnailUri));
        } else {
            thumbnail.setImageResource(R.drawable.ic_launcher);
        }

        return v;
    }

But when I try to parse the Uri that is stored in thumbnailUri, I receive the following error:

   08-09 01:58:38.619: I/System.out(1471): resolveUri failed on bad bitmap uri: content://com.android.contacts/contacts/1/photo

Am I going about this the wrong way? Any help would be greatly appreciated!

解决方案

private Uri getPhotoUriFromID(String id) {
    try {
        Cursor cur = getContentResolver()
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + id
                                + " 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, Long.parseLong(id));
    return Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

This is the function where you need to pass contact id and you will get the URI of the image which you can set easily in the imageview.

use the response uri of this function like imageView.setImageURI(uri)

Hope it will work for your code.

这篇关于如何获得Android的联系缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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