How to find the maximum image size for contact images?

background

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

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

and before that, the contact photos had only a thumbnail size - 96x96

question

Is there any function in the API that returns the maximum size of the contact image?

I also hope that the manufacturers did not change the maximum image sizes, and even if they did, and we have such a function, we will return the correct size to us.

+6
source share
2 answers

According to this link , the correct way to get the maximum size:

@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; } 

UPDATE: if we use at least API 16 (4.1), you can 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 } 
+5
source

From the announcement :

With Android 4.1, you can store photos of contacts up to 720 x 720 in size, making contacts even richer and more personal. Applications can store and receive photos of contacts with this size or use any other size. The maximum size of photos supported on specific devices may vary, so applications must request the built-in contact provider at runtime to get the maximum size for the current device.

I would request for ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI . He must return what you seek.

+1
source

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


All Articles