Updating / customizing your own user profile profile

I know how to get user profile from ContentResolver . If I have a bitmap, how can I set it as a user profile image (replace it or install it if it does not exist)?

I load the user profile as follows:

 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(); } } 
+4
source share
1 answer

Here, how to update or create profile images, in fact, it works the same way as updating regular contact images. I had a problem somewhere else ...

Instead of using my UserProfile, just replace them and pass 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); } ... } 
0
source

All Articles