小编Pau*_*ulH的帖子

如何加载联系人照片?

我在Android中为联系人加载照片时遇到问题.我已经google了一个答案,但到目前为止已经空了.有没有人有查询联系人,然后加载照片的例子?

因此,给定一个来自一个名为using的Activity结果的contactUri

startActivityForResult(new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI),PICK_CONTACT_REQUEST) 
Run Code Online (Sandbox Code Playgroud)

是:

内容://com.android.contacts/data/1557

loadContact(..)工作正常.但是,当我调用getPhoto(...)方法时,我得到了照片InputStream的空值.它也令人困惑,因为URI值不同.contactPhotoUri评估为:

内容://com.android.contacts/contacts/1557

请参阅下面代码中的内联注释.

 class ContactAccessor {

    /**
     * Retrieves the contact information.
     */
    public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {

        //contactUri --> content://com.android.contacts/data/1557

        ContactInfo contactInfo = new ContactInfo();

        // Load the display name for the specified person
        Cursor cursor = contentResolver.query(contactUri,
                                            new String[]{Contacts._ID, 
                                                         Contacts.DISPLAY_NAME, 
                                                         Phone.NUMBER,
                                                         Contacts.PHOTO_ID}, null, null, null);
        try {
            if (cursor.moveToFirst()) {
                contactInfo.setId(cursor.getLong(0));
                contactInfo.setDisplayName(cursor.getString(1));
                contactInfo.setPhoneNumber(cursor.getString(2));
            }
        } finally {
            cursor.close();
        }        
        return contactInfo;  // <-- returns info for contact
    }

    public Bitmap …
Run Code Online (Sandbox Code Playgroud)

android contactscontract

43
推荐指数
5
解决办法
8万
查看次数

标签 统计

android ×1

contactscontract ×1