如何加载联系人照片?

时间:2014-06-23 12:45:51

标签: android

我创建了一个应用程序,用他/她的号码显示一张联系人照片,但我的结果只是一个空白页面,这是我的main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <QuickContactBadge
        android:id="@+id/quickContactBadge1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

这是我的MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String phoneNumber = "XXXXXXXXXXX";
        QuickContactBadge badge = (QuickContactBadge) findViewById(R.id.quickContactBadge1);
        new QuickContactHelper(this, badge, phoneNumber).addThumbnail();
    }



}

这是QuickContactHelper.java

public class QuickContactHelper {

    private static final String[] PHOTO_ID_PROJECTION = new String[] { ContactsContract.Contacts.PHOTO_ID };

    private static final String[] PHOTO_BITMAP_PROJECTION = new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO };

    private final QuickContactBadge badge;

    private final String phoneNumber;

    private final ContentResolver contentResolver;

    public QuickContactHelper(final Context context,
            final QuickContactBadge badge, final String phoneNumber) {

        this.badge = badge;
        this.phoneNumber = phoneNumber;
        contentResolver = context.getContentResolver();

    }

    public void addThumbnail() {

        final Integer thumbnailId = fetchThumbnailId();
        if (thumbnailId != null) {
            final Bitmap thumbnail = fetchThumbnail(thumbnailId);
            if (thumbnail != null) {
                badge.setImageBitmap(thumbnail);
            }
        }

    }

    private Integer fetchThumbnailId() {

        final Uri uri = Uri.withAppendedPath(
                ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        final Cursor cursor = contentResolver.query(uri, PHOTO_ID_PROJECTION,
                null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");

        try {
            Integer thumbnailId = null;
            if (cursor.moveToFirst()) {
                thumbnailId = cursor.getInt(cursor
                        .getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
            }
            return thumbnailId;
        } finally {
            cursor.close();
        }

    }

    final Bitmap fetchThumbnail(final int thumbnailId) {

        final Uri uri = ContentUris.withAppendedId(
                ContactsContract.Data.CONTENT_URI, thumbnailId);
        final Cursor cursor = contentResolver.query(uri,
                PHOTO_BITMAP_PROJECTION, null, null, null);

        try {
            Bitmap thumbnail = null;
            if (cursor.moveToFirst()) {
                final byte[] thumbnailBytes = cursor.getBlob(0);
                if (thumbnailBytes != null) {
                    thumbnail = BitmapFactory.decodeByteArray(thumbnailBytes,
                            0, thumbnailBytes.length);
                }
            }
            return thumbnail;
        } finally {
            cursor.close();
        }

    }

}

这是读取联系人的权限:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

现在我的错误是什么,没有异常没有错误,我总是得到一个空白页面 那么如何处理这个错误? 这是我的结果:

Blank Page

0 个答案:

没有答案