如何获取联系人照片并在API 23中设置为imageview

时间:2017-03-21 13:50:19

标签: android android-6.0-marshmallow android-contacts

我正在使用Contact.Picker并使用下面的代码来获取联系号码,我想如果联系人有图像然后将其设置为我的图像视图,我尝试了很多方法,但他们都没有使用某些正在使用位图的一些Uri ,但他们都没有在我的棉花糖中工作请帮助!!!!

   case (PICK_CONTACT):
            Cursor cursor = null;
            String phoneNumber = "";
            String image="";
            List<String> allNumbers = new ArrayList<String>();
            int phoneIdx = 0;
            String photoId = "";
            try {
                Uri result = data.getData();
                String id = result.getLastPathSegment();
                cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id}, null);
                phoneIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);

                if (cursor.moveToFirst()) {
                    while (cursor.isAfterLast() == false) {
                        phoneNumber = cursor.getString(phoneIdx);

                        allNumbers.add(phoneNumber);

                        cursor.moveToNext();
                    }
                } else {
                    //no results actions
                }
            } catch (Exception e) {
                //error actions
            } finally {
                if (cursor != null) {
                    cursor.close();
                }

                final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Choose a number");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        String selectedNumber = items[item].toString();

String selectedNumber = phoneNumber.toString();
//Contact photo pick code
                        Bitmap hello=  retrieveContactPhoto(MainActivity.this,selectedNumber);
                    avatar.setImageBitmap(hello);
                    Log.d("Bitmapis",hello+"");

                        selectedNumber = selectedNumber.replace("-", "");
                        number.setText(selectedNumber);
                    }
                });
                AlertDialog alert = builder.create();
                if (allNumbers.size() > 1) {
                    alert.show();
                } else {
                    String selectedNumber = phoneNumber.toString();
enter code here

                    selectedNumber = selectedNumber.replace("-", "");

                    number.setText(selectedNumber);
                }

                if (phoneNumber.length() == 0) {
                    //no numbers found actions
                }
            }
            break;
    }

  public static Bitmap retrieveContactPhoto(Context context, String number) {
    ContentResolver contentResolver = context.getContentResolver();
    String contactId = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};

    Cursor cursor =
            contentResolver.query(
                    uri,
                    projection,
                    null,
                    null,
                    null);

    if (cursor != null) {
        while (cursor.moveToNext()) {
            contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        }
        cursor.close();
    }

    Bitmap photo = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.man);

    try {
        InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
                ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactId)));

        if (inputStream != null) {
            photo = BitmapFactory.decodeStream(inputStream);
        }

        assert inputStream != null;
        inputStream.close();

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

1 个答案:

答案 0 :(得分:0)

你可以尝试两件事:

  1. docs中,有两个示例代码,一个用于缩略图大小的照片,另一个用于高质量的照片。联系人可能有一个但不是另一个,所以我建议您在代码中尝试这两个选项。
  2. 您似乎从CONTACT_ID意图中获得了PICK_CONTACT,将其转换为手机,然后在retrieveContactPhoto中将您的手机转换回来使用CONTACT_IDPhoneLookup。这并不能保证您获得原始CONTACT_ID,您可以通过同一部手机获得其他联系,我建议将原始ID传递给retrieveContactPhoto方法。