【问题标题】:Get selected contact image - Android获取选定的联系人图片 - Android
【发布时间】:2015-05-29 10:52:58
【问题描述】:

我正在尝试获取联系人图像,但似乎无法使其正常工作。我已经阅读了其他问题,但没有一个能够解决我的问题。

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

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 始终为空,谁能告诉我如何以正确的方式获取联系人照片?

谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以使用此方法将联系人的照片加载为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;
    }
    

    【讨论】:

    • 也试过了,但 inputstream 再次为空,我也有` ` 但它不起作用
    • 您确定您使用的是正确的contactID,因为我没有在您的代码中看到您设置contactID 的位置吗?
    猜你喜欢
    • 2014-01-19
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多