Is This How To Correctly Set A Photo To A Contact On The Address-book?
Background Suppose I've found a contact by using some query on the address book. Found it by performing a query to get contact info from a specific account (like of WhatsApp), as I
Solution 1:
I see a number of possible issues in the code:
- You're assuming there's an existing photo stored for that contact (you're using
newUpdate
), in case there is not currently a photo, you should donewInsert
instead. - You should be updating/inserting on a specific RawContact, what you wrote updates all that Contact's RawContacts photo data raws, some RawContacts might have read-only status, and will not allow your app to modify it.
- Handling photos, especially when storing them into the Contacts DB is a very memory intense action, it might crash on
OutOfMemoryError
for some devices, either because they have very limited RAM or because the memory is currently heavily used by something else. You should wrap your entire code including the applyBatch with try/catch, and catch all Exceptions/Errors and report them to the log, and preferably to your error-reporting library as well (Firebase/crashlytics/etc) - You're only updating the
PHOTO
field of the data raw, you should also updatePHOTO_FILE_ID
field, as some apps may choose to read thePHOTO_FILE_ID
and get the photo from it instead. - You're not setting your new photo as the
SUPER_PRIMARY
of its contact, so if some other RawContact in that same Contact also has a photo, it might "win", and be the primary photo for that contact.
Here's code that should help:
// To simplify the code i didn't worry about try/catch, freeing resources, // or running slow queries on the main thread, make sure your code does!public ContentProviderOperation setPhoto(int contactId, byte[] photoByteArray) {
longrawId= getRawId(contactId);
ContentProviderOperation.Builder builder;
Stringselection= Data.RAW_CONTACT_ID + " = '" + rawId + "' AND " + Data.MIMETYPE + "= '" + Photo.CONTENT_ITEM_TYPE + "'";
Cursorcur= contentResolver.query(Data.CONTENT_URI, newString[]{ Data._ID }, selection, null, null);
if (cur.moveToFirst()) {
// this RawContact already has a photo!LongphotoDataId= cur.getLong(0);
Uriuri= ContentUris.withAppendedId(Data.CONTENT_URI, photoDataId);
builder = ContentProviderOperation.newUpdate(uri);
} else {
// this RawContact has no photo.
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValue(Data.RAW_CONTACT_ID, rawId);
}
builder.withValue(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
builder.withValue(Data.IS_SUPER_PRIMARY, 1);
builder.withValue(Data.IS_PRIMARY, 1);
builder.withValue(Photo.PHOTO, photoByteArray);
builder.withValue(Photo.PHOTO_FILE_ID, null);
return builder.build();
}
privatelonggetRawId(long contactId) {
Stringselection= RawContacts.CONTACT_ID + "='" + contactId + "'";
Cursorcur= contentResolver.query(RawContacts.CONTENT_URI, newString[]{ RawContacts._ID }, selection, null, null);
try {
if (cur.moveToNext()) {
return cur.getLong(0);
}
} finally {
cur.close();
}
return0;
}
Post a Comment for "Is This How To Correctly Set A Photo To A Contact On The Address-book?"