7

background

starting with jelly bean (4.1), android now supports contact images that are 720x720 .

before, starting with ICS (4.0), android has supported contact images that are 256x256.

and before that, contact photos had just a size of a thumbnail - 96x96

the question

is there any function in the API that returns the max size of the contact image?

i also hope that the manufacturers didn't change the max image sizes, and even if they did and we have such a function, it would return us the correct size.

android developer
  • 114,585
  • 152
  • 739
  • 1,270

2 Answers2

5

according to this link, the correct way to get the max size is:

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static int getMaxContactPhotoSize(final Context context) {
    if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
        // Note that this URI is safe to call on the UI thread.
        final Uri uri = ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI;
        final String[] projection = new String[] { ContactsContract.DisplayPhoto.DISPLAY_MAX_DIM };
        final Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        try {
            c.moveToFirst();
            return c.getInt(0);
        } finally {
            c.close();
        }
    }
    // fallback: 96x96 is the max contact photo size for pre-ICS versions
    return 96;
}

EDIT: if we use at least API 16 (4.1), it's possible to use something like:

@AnyThread
@RequiresPermission(anyOf = [Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS])
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
fun getMaxContactPhotoSize(context: Context): Int {
    // Note that this URI is safe to call on the UI thread.
    if (contactMaxPhotoSize > 0)
        return contactMaxPhotoSize
    val uri = ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI
    val projection = arrayOf(ContactsContract.DisplayPhoto.DISPLAY_MAX_DIM)
    context.contentResolver.query(uri, projection, null, null, null)?.use { cursor ->
        cursor.moveToFirst()
        contactMaxPhotoSize = cursor.getInt(0)
    }
    if (contactMaxPhotoSize > 0)
        return contactMaxPhotoSize
    // fallback: 720x720 is the max contact photo size for 4.1 version
    contactMaxPhotoSize = 720
    return contactMaxPhotoSize
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • So... what I said, then? Cool. – Geobits Jul 25 '13 at 15:04
  • yes, and if you wish that i tick your answer, please copy this code and link to your answer. – android developer Jul 25 '13 at 18:11
  • code vs no-code. That is the question ;D. In any case two good answers and both got my upvote – leRobot Dec 10 '14 at 10:46
  • @leRobot Do you know perhaps about the same thing, yet for contact thumbnails ? Meaning getting the max dimensions that you get for thumbnails? Is it maybe the same, but with " THUMBNAIL_MAX_DIM" instead ? – android developer Nov 12 '15 at 08:43
  • that's what the docs say for ContactsContract.DisplayPhoto.THUMBNAIL_MAX_DIM: _Queries to (...) will contain this column, populated with the height and width (in pixels) for photo thumbnails._ As a personal note, in my app I'm only setting the display photo. I believe the system provider handles thumbnails automagically if they don't exist but the Display one does – leRobot Nov 12 '15 at 11:33
  • @leRobot OK thank you. Say, have you used "openContactPhotoInputStream" with "true" as the second value? On random cases, it shows a bad quality image (actually a thumbnail), even though for the same exact contact, on another call to this function, it shows a good quality image. I've noticed it on Nexus 4 with 5.1.1, but I can't see it happen on Android 6. – android developer Nov 12 '15 at 11:56
  • 1
    that's weird because if for the same contact you're calling the last parameter boolean version of openContactPhotoInputStream, it should always return the high-res pic. I would advise attempting to get the PHOTO_ID and/or PHOTO_FILE_ID to manually query for the Uri, since the open... method might be broken accross SDKs. Link below http://developer.android.com/reference/android/provider/ContactsContract.ContactsColumns.html#PHOTO_ID – leRobot Nov 12 '15 at 14:04
  • @leRobot Indeed. It's very weird, as it happened multiple times today, and now I can't reproduce it. So you say that the PHOTO_ID and/or PHOTO_FILE_ID could be used instead? Do you have a recommended sample for this? – android developer Nov 12 '15 at 14:53
  • 1
    I have never done it but I google it (ended up on SF, where else :D). [This guy](http://stackoverflow.com/a/13119336/1103974) seems to be doing it. See his queryContactImage method that feeds from the PHOTO_ID from the query on queryContactInfo. He then just creates a bitmap from the query. Note you can do a similar thing for PHOTO_THUMBNAIL_URI – leRobot Nov 12 '15 at 15:16
  • 1
    Oh he also mentions some API convenience class I once looked at but didn't remember until now. It provides insight on the examples on how the contact photos are stored in a read-only directory for each contact, and how to get the two pics (thumb and full res) the system stores: http://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html – leRobot Nov 12 '15 at 15:19
  • @leRobot Nice. Thank you again. – android developer Nov 13 '15 at 08:06
2

From the announcement:

With Android 4.1, you can store contact photos that are as large as 720 x 720, making contacts even richer and more personal. Apps can store and retrieve contact photos at that size or use any other size needed. The maximum photo size supported on specific devices may vary, so apps should query the built-in contacts provider at run time to obtain the max size for the current device.

I would query for ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI. It should return what you're looking for.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Correct me if I'm wrong, but if you're under API 14, it would be 96x96 and you don't need to query, right? – Geobits Jul 24 '13 at 15:18
  • i've heard that some manufacturers used higher resolutions for their contacts even on gingerbread. i'm not sure where i've got this information from though. might be wrong. – android developer Jul 24 '13 at 16:17
  • Even if that's true, you'd have to know each manufacturer's custom query, since there is no standard one before then. That could end up being a tangle mess that I'd probably avoid, and just go with the default for users <14. – Geobits Jul 24 '13 at 16:32