Display contact photo photo_ID

I allow the user to select a contact in my application, and I show it on the main screen, but the photo does not appear, and I do not know what happened.

This is how I get the link to the photo:

... Cursor c = null; try { c = getContentResolver().query(uri, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.PHOTO_ID }, null, null, null); if (c != null && c.moveToFirst()) { String number = c.getString(0); int type = c.getInt(1); String name = c.getString(2); int photo = c.getInt(3); showSelectedNumber(type, number, name, photo); } } 

This is how I show it:

 public void showSelectedNumber(int type, String number, String name, int photo) { mAppWidgetPrefix.setText(name); pickedNumber.setText(number); pickedPhoto.setImageResource(photo); } 

Why doesn't it work?

+4
source share
1 answer

You are trying to set the row identifier from the ContactsContract.Data table as the resource identifier in your ImageView . And, of course, this will not work. It doesn't even make any sense.

First you must first download the original photo from the database, and then you can show it.

For example, you can use this code to extract a raster image of an image using a line identifier pointing to image data (I updated some code fragments to check it):

 private void queryContactInfo(int rawContactId) { Cursor c = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.PHOTO_ID }, ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[] { Integer.toString(rawContactId) }, null); if (c != null) { if (c.moveToFirst()) { String number = c.getString(0); int type = c.getInt(1); String name = c.getString(2); int photoId = c.getInt(3); Bitmap bitmap = queryContactImage(photoId); showSelectedNumber(type, number, name, bitmap); } c.close(); } } private Bitmap queryContactImage(int imageDataRow) { Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO }, ContactsContract.Data._ID + "=?", new String[] { Integer.toString(imageDataRow) }, null); byte[] imageBytes = null; if (c != null) { if (c.moveToFirst()) { imageBytes = c.getBlob(0); } c.close(); } if (imageBytes != null) { return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); } else { return null; } } public void showSelectedNumber(int type, String number, String name, Bitmap bitmap) { mInfoView.setText(type + " " + number + " " + name); mImageView.setImageBitmap(bitmap); // null-safe } 

You can also see http://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html as a convenient directory of suppliers for receiving contact photos. There is also an example.

+11
source

Source: https://habr.com/ru/post/950188/


All Articles