android - 从联系人获取照片

标签 android

好的,我只是想了解如何使用联系信息,但我有点卡住了。我希望能够显示联系人的图片。使用我拥有的以下代码,我如何才能将联系人的照片放入 contact_entry 的 ImageView 中?

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>

最佳答案

这可能会对您有所帮助(联系人由 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);
}

用法是:

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

关于android - 从联系人获取照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18701024/

相关文章:

android - 在 LocationListener 中获取上下文

android - APK 与最新的 Android 版本不兼容

Android Debug Bridge (adb) 设备 - 无权限

安卓通知 : onMessageReceived not called

java - 尝试获取签名的 apk 时 Android Studio 出现 "Got negative length for block"错误

android - 如何在 Jetpack Compose 中让底页覆盖整个屏幕

android - 提供一个高分辨率可绘制 PNG Assets 并让 Android 缩小它是否更容易?

android - ListView 的动态行布局

android - 为什么模拟位置跳回真实位置

android - 在操作栏顶部显示 View