4

I know how to retrieve a user profile from the ContentResolver. If I have a bitmap, how can I set it as a user profile picture (replace it OR set it, if none exists)?

I load the user profile like following:

Uri dataUri = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
String[] selection = new String[]
{
        ContactsContract.Profile._ID,
        ContactsContract.Profile.DISPLAY_NAME,
        ContactsContract.Profile.PHOTO_URI,
        ContactsContract.Profile.LOOKUP_KEY
};

Cursor cursor = MainApp.get().getContentResolver().query(
        dataUri,
        selection,
        null,
        null,
        null);

if (cursor != null)
{
    int id = cursor.getColumnIndex(ContactsContract.Profile._ID);
    int name = cursor.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME);
    int photoUri = cursor.getColumnIndex(ContactsContract.Profile.PHOTO_URI);
    int lookupKey = cursor.getColumnIndex(ContactsContract.Profile.LOOKUP_KEY);

    try
    {
        if (cursor.moveToFirst())
        {
            int phId = cursor.getInt(id);
            mName = cursor.getString(name);
            mImageUri = cursor.getString(photoUri);
            mLookupKey = cursor.getString(lookupKey);

            mExists = true;
        }
    }
    finally
    {
        cursor.close();
    }
}
prom85
  • 16,896
  • 17
  • 122
  • 242
  • Maybe this will help ? http://stackoverflow.com/questions/17789256/change-contact-picture-programmatically – 2red13 Sep 21 '15 at 06:39
  • Thanks. Actually, it's somehow different to do that with contacts and the user profile... BUT I just figured out how it works – prom85 Sep 21 '15 at 07:18
  • you want to add a bitmap to your contact? – Elltz Sep 24 '15 at 22:17
  • to my PROFILE, not a contact... but as I said, I know how to do it now... – prom85 Sep 25 '15 at 05:49
  • 1
    @prom85 If you figured it out, consider posting an answer to your question with the solution – wasyl Sep 27 '15 at 18:27
  • I will after the bounty is over... I don't want to take away others to get the points as long as it's possible to get points... – prom85 Sep 27 '15 at 18:29
  • I'm trying to do something very similar (essentially trying to insert a profile contact if there isn't one) but I'm failing. Any ideas how to do it? – Pratik Thaker Nov 29 '15 at 17:12
  • I asnwered the question with my solution for you... – prom85 Nov 29 '15 at 19:02

1 Answers1

0

Here's how to update or create profile images, actually it's working the same way as updating normal contact pictures. I had a problem somewhere else...

Instead of using my UserProfile, just exchange them and hand on the raw id.

private static void updatePhoto(UserProfile profile, Bitmap bitmap, ...)
{
    byte[] photo = ImageUtil.convertImageToByteArray(bitmap, true);

    ContentValues values = new ContentValues();
    int photoRow = -1;
    String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + profile.getRawId() + " AND " + ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
    Cursor cursor = MainApp.get().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
    int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
    if (cursor.moveToFirst()) {
        photoRow = cursor.getInt(idIdx);
    }
    cursor.close();

    values.put(ContactsContract.Data.RAW_CONTACT_ID, profile.getRawId());
    values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
    values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo);
    values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);

    if (photoRow >= 0) {
        MainApp.get().getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
    } else {
        MainApp.get().getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    }

    ...
}
prom85
  • 16,896
  • 17
  • 122
  • 242