得到一个图片从联系 [英] Getting a Photo from a Contact

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

问题描述

好吧,我只是想了解如何使用联系人的信息,但我有点卡住了。我想是能够显示联系人的图片。使用下面的code,我有,我将如何能够把照片在contact_entry接触的ImageView的?

 的ListView contacts_list =(ListView控件)findViewById(R.id.contacts_list);

//获取数据库的URI
开放的我们的uri = ContactsContract.Contacts.CONTENT_URI;
//什么,从数据库抓取
的String []投影=新的String [] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.Contacts.PHOTO_ID
};

字符串排序顺序= ContactsContract.Contacts.DISPLAY_NAME +分页中局部ASC;

光标光标= managedQuery(URI,投影,NULL,NULL,排序顺序);

的String []字段=新的String [] {
        ContactsContract.Data.DISPLAY_NAME
};

INT []值= {
        R.id.contactEntryText
};

SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.contact_entry,光标,
        字段值);
contacts_list.setAdapter(适配器);
 

contact_entry.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =54px>
    < ImageView的
        机器人:ID =@ + ID / contactPhoto
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/ ic_contact_picture_3/>
    <的TextView
        机器人:文本=@ + ID / contactEntryText
        机器人:ID =@ + ID / contactEntryText
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 

解决方案

也许这将帮助你(接触是通过的getId标识()):

  / **
 返回:照片URI
 * /
公共乌里getPhotoUri(){
    尝试 {
        光标CUR = this.ctx.getContentResolver()查询(
                ContactsContract.Data.CONTENT_URI,
                空值,
                ContactsContract.Data.CONTACT_ID +=+ this.getId()+和
                        + ContactsContract.Data.MIMETYPE +=
                        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE +',空,
                空值);
        如果(CUR!= NULL){
            如果(!cur.moveToFirst()){
                返回null; //没有照片
            }
        } 其他 {
            返回null; //在光标过程中的错误
        }
    }赶上(例外五){
        e.printStackTrace();
        返回null;
    }
    乌里人= ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,龙
            .parseLong(的getId()));
    返回Uri.withAppendedPath(人,ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
 

用法是:

 开放的U = objItem.getPhotoUri();
如果(U!= NULL){
        mPhotoView.setImageURI(U);
} 其他 {
        mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}
 

Alright, I'm just trying to learn about using Contact information, but I'm a bit stuck. I would like to be able to display a picture for the contact. Using the following code that I have, how would I be able to put the photo for the contact in the ImageView in contact_entry?

ListView contacts_list = (ListView) findViewById(R.id.contacts_list);

// Gets the URI of the db
Uri uri = ContactsContract.Contacts.CONTENT_URI;
// What to grab from the db
String[] projection = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.Contacts.PHOTO_ID
};

String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

Cursor cursor = managedQuery(uri, projection, null, null, sortOrder);

String[] fields = new String[] {
        ContactsContract.Data.DISPLAY_NAME
};

int[] values = { 
        R.id.contactEntryText
};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
        fields, values);
contacts_list.setAdapter(adapter);

contact_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="54px">
    <ImageView
        android:id="@+id/contactPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_contact_picture_3"/>
    <TextView 
        android:text="@+id/contactEntryText"
        android:id="@+id/contactEntryText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

解决方案

Probably this will help you(contact is identified by getId()):

/**
 * @return the photo URI
 */
public Uri getPhotoUri() {
    try {
        Cursor cur = this.ctx.getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " 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(getId()));
    return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

Usage is:

Uri u = objItem.getPhotoUri();
if (u != null) {
        mPhotoView.setImageURI(u);
} else {
        mPhotoView.setImageResource(R.drawable.ic_contact_picture_2);
}

这篇关于得到一个图片从联系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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