📜 ⬆️ ⬇️

Work with contact photos in android 2.1+

In the course of learning programming under the Android system, I was given the task of writing my contact manager, albeit not as a replacement for the system manager, but as an educational task.
The work with the contact list has already been described here , and more than once, because I will write only about what I have not met here, namely, about working with a contact photo (contact avatar).

In the course of the work I wrote the helper class, the code can be seen here . In it were implemented the main functions for working with photos from the contact.
There are two main methods:
public static void setContactPhoto(ContentResolver c, byte[] bytes, long personId)
public static Bitmap loadContactPhoto(ContentResolver cr, long id, Context context)
As is obvious from the name, in the first method we save the contact a new photo, and the second method loads the photo from the contact database.

Using methods:
To save a photo to a contact, you must first receive it, for example, from the gallery.
Suppose we have an ImageButton button on the activation, by pressing which the window for selecting a photo from the gallery will appear. Do not forget that the photo in the gallery can be of any size, because, first, the photo must be reduced to the desired size. Fortunately, the programmer for android does not require knowledge of the algorithms for reducing the size and cropping images, everything has already been done by Google in advance, it’s enough to add the necessary parameters to the call, example below:
 private static final int REQ_CODE_PICK_IMAGE = 1; private Bitmap contactPhoto = null; private ImageView ibtnAvatar; @Override protected void onCreate(Bundle bundle) { ... ibtnAvatar = (ImageButton) findViewById(R.id.ibtn_avatar); ibtnAvatar.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); i.putExtra("crop", "true"); i.putExtra("aspectX", 1); i.putExtra("aspectY", 1); i.putExtra("outputX", 500); i.putExtra("outputY", 500); i.putExtra("scale", true); i.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); i.putExtra("noFaceDetection", false); i.putExtra("return-data", false); i.putExtra(MediaStore.EXTRA_OUTPUT, ContactPhotoHelper.getTempUri()); startActivityForResult(i, REQ_CODE_PICK_IMAGE); } }); } 

As we can see from this code, when creating a new intent, we added parameters from the documentation to it, for automatic cutting to size and reducing the photo. The result of the execution of the system activite will put, according to the standard, to the onActivityResult call that caused its activation.
It should be noted that in this example, system activation will put the result in a temporary file that we created in the ContactPhotoHelper.getTempUri () method.
Processing of the result: the resulting photo will be highlighted on the button
  @Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch (requestCode) { case REQ_CODE_PICK_IMAGE: if (resultCode == RESULT_OK) { contactPhoto = BitmapFactory.decodeFile(ContactPhotoHelper.getTempFile() .getAbsolutePath()); ibtnAvatar.setImageBitmap(contactPhoto); } } } 

')
After that, you can save the user in this profile picture:
  if (contactPhoto != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); contactPhoto.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); ContactPhotoHelper.setContactPhoto(getContentResolver(), bitmapdata, contactId); } 

As a result, the selected contact receives a new photo.

To download a photo from the contact list, the call is quite simple, we use the same ImageButton button:
 ibtnAvatar.setImageBitmap(ContactPhotoHelper.loadContactPhoto(getContentResolver(), contactId, this)); 

Thus, using only two static methods, I greatly simplified working with contact photos, I don’t know why it wasn’t done by Google developers.
Thanks for attention.

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


All Articles