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've written here.
Now I have an image that I wish to use it to update the contact's photo.
The problem
I have created code based on things I've found here on StackOverflow, that update a contact photo.
Thing is, some users claim it doesn't do anything. I'm not sure what causes it. Maybe bad way to access the contact (id/lookup-key?). Maybe I need to get an extra address-book field. Maybe the query itself is wrong...
I'm having problems figuring out the reason for this issue, because I can't reproduce it and so does most of the users.
The code
Here's what I did:
final ArrayList<ContentProviderOperation> ops = new ArrayList<>();
final String lookupKey=...;
try {
FileInputStream fileInputStream = new FileInputStream(file);
final byte[] photoByteArray = new byte[(int) file.length()];
fileInputStream.read(photoByteArray);
fileInputStream.close();
final Builder builder = ContentProviderOperation.newUpdate(Data.CONTENT_URI);
builder.withSelection(Data.LOOKUP_KEY + "=?" + " AND " + Data.MIMETYPE + "=?", new String[]{lookupKey, Photo.CONTENT_ITEM_TYPE});
builder.withValue(Photo.PHOTO, photoByteArray);
ops.add(builder.build());
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops )
The question
Is there anything wrong with the code?
Should I update the contact differently?
How come some users fail to update? Isn't the lookup-key enough? Can it update the wrong data?
I've read somewhere that I might need to use RAW-contact-ids, but couldn't find what to do exactly. Query and then update/insert?
Someone has suggested that developers should consider using an account of their app to save photos, instead of updating, as it might cause issues. Is it true?