获取选定的联系人图片 - Android

时间:2015-03-25 16:29:48

标签: android android-contentprovider android-contacts android-contentresolver

我试图获取联系人图片,但似乎我无法让这个工作。我已经阅读了其他问题,但没有一个能够解决我的问题。

以下是我的工作,我通过以下方式检索所选联系人:

Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);

并检索数据:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == PICK_CONTACT_REQUEST) {

            if (resultCode != 0) {

                Log.d(TAG, resultCode + " result");
                uriContact = intent.getData();
                Log.d(TAG, resultCode + " result - " + intent.getData());
                // handle the picked phone number in here.
                String number = GetPhoneNumber();
                getContactPhoto();
                String name = getContactName();
                if (contacts.size() > 0) {
                    for (Contact contact : contacts) {
                        if (contact.getContactID().equals(contactID))
                            return;
                    }
                }
                contacts.add(new Contact(name, contactID, number));
                adapter = new ContactAdapter(getActivity(), (ArrayList<Contact>) contacts);
                ((ListView) view.findViewById(R.id.emergency_contact_list)).setAdapter(adapter);
                view.findViewById(R.id.emergency_contact_done).setEnabled(true);
            }
        }
    }

以下是我尝试显示联系人图片的方式:

    private void getContactPhoto() {

            Bitmap photo = null;
            try {
//inputStream is always null - why so?
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
                Log.d(TAG, "input stream " + inputStream);
                if (inputStream != null) {
                    photo = BitmapFactory.decodeStream(inputStream);
                    ImageView imageView = (ImageView) view.findViewById(R.id.testimg);
                    imageView.setImageBitmap(photo);
                    assert inputStream != null;
                    inputStream.close();
                }


            } catch (IOException e) {
                e.printStackTrace();
            }
        }

正如我在getContactPhoto方法中注意到的那样,inputStream始终为null,有人能告诉我如何以道具方式获取联系人照片?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用此方法将联系人的照片加载为byte[]

public static byte[] loadIcon(Context context, long contactId, boolean highRes) {
    byte[] icon = null;
    // Load the icon
    if (contactId <= 0) {
        return icon;
    }
    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
    try {
        InputStream is = null;
        is = Contacts.openContactPhotoInputStream(context.getContentResolver(), contactUri, highRes);
        if (is != null) {
            int iconSize = is.available();
            // this is if you want to avoid loading large pictures
            if (iconSize > 200 * 1024) {
                Log.d(TAG, "load contact. Icon too big: " + iconSize);
            } else {
                icon = new byte[iconSize];
                is.read(icon, 0, icon.length);
            }
            is.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return icon;
}