如何使用Android的同步通过Facebook上的联系人图片 [英] How to use the contact pictures synced by Facebook for Android

查看:110
本文介绍了如何使用Android的同步通过Facebook上的联系人图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的手机自动synces在我的联系人列表中的人的FB我的照片我的手机具有的Facebook为Android。

I'm having Facebook for Android on my phone which automatically synces the FB profile pictures of the people in my contact list to my phone.

我想用这些照片在我的应用程序,我访问 ContactsContract.PhoneLookup

I'd like to use those pictures within my app where I access ContactsContract.PhoneLookup.

我真的需要Facebook的SDK来做到这一点?我想不会,但我无法找到图片的任何证据被保存周围 ContactsContract

Do I really need the Facebook SDK to do that? I guess not, but I cannot find any evidence of the pictures being saved somewhere around ContactsContract

推荐答案

您只需查询该照片的URI,并使用URI根据您的需求。

You simply need to query for the Photo URI and use the URI as per your needs.

此方法会帮助你。

/**
 * @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);
}

或者还有另一种方法:

Or there is another approach:

public Uri getPhotoUri(Integer contactid) {
    Cursor photoCur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'", null, ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC");
    photoCur.moveToPosition(contactid);
    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, photoCur.getLong(photoCur.getColumnIndex(ContactsContract.Contacts._ID)));
    Uri photo = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    return photo;
}

请注意,该URI记录可能不存在。例如,如果图像存储在SD卡,而不是present,该URI将返回没有照片了。为此,您需要做进一步检查。

Please note that the URI record may not exists. If the images for example are stored on the SDCard and that is not present, the URI will return no photo about it. For this you need to do further checks.

这将有助于你还(如果开放的失败,它设置一个默认的占位符图片):

This will help you also (if Uri fails it sets a default placeholder image):

和调用该函数(contactimage是一个ImageView的):

and calling that function (contactimage is an ImageView):

Uri contactphoto = objContact.getPhotoUri();
contactimage.setImageURI(contactphoto);
try {
    String nullString = contactimage.getDrawable().toString();
} catch (java.lang.NullPointerException ex) {
    contactimage.setImageResource(R.drawable.contactplaceholder);
}

这篇关于如何使用Android的同步通过Facebook上的联系人图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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