Am I the only one trying to achieve this ... :/ ?
In short:
I want to fetch back the picture of my contacts as they do defined it by themselves (on their Google own Account page for instance).
Use case: I have modified one of my contact's picture myself, and now, I want to undo that change -> I want to 'fetch back' the Google picture of my contact (the one set by him/herself).
I have an app that manage Google Contacts. It also manage contact photo using
ContactsContract.CommonDataKinds.Photo.PHOTO
And it's working fine.
Here is a scenario I would like to support:
Please take a look at my code here to set the Photo. Should I just 'clear' the photo and relies on ContactProvider to download back user photo from Google account?
How can I clear the photo. Set ContactsContract.CommonDataKinds.Photo.PHOTO to 'null'? and delete the associated file, i.e.,
Uri rawContactPhotoUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
Thanks for helping.
Here is how I update picture:
private void updatePhotoThumbnail(Bitmap bitmap, Contact contact) throws Exception
{
byte[] contactPhotoBytes = getContactPhotoBytes(bitmap);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// @formatter:off
String where = ContactsContract.RawContacts.ACCOUNT_NAME + "= ? "
+ "AND " + ContactsContract.RawContacts.ACCOUNT_TYPE + "= ? "
+ "AND " + ContactsContract.Data.CONTACT_ID + "= ? "
+ "AND " + ContactsContract.Data.RAW_CONTACT_ID + "= ? "
+ "AND " + ContactsContract.Data.MIMETYPE + " = ?";
// @formatter:on
String[] params = new String[]
{
// @formatter:off
_accountName,
AccountManagerHelper.GOOGLE_ACCOUNT_TYPE,
String.valueOf(contact.getId()),
String.valueOf(contact.getRawContactId()),
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
// @formatter:on
};
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(where, params)
.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, contactPhotoBytes).build());
try
{
_contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
updateDisplayPhoto(contact.getRawContactId(), contactPhotoBytes);
}
catch (RemoteException e)
{
e.printStackTrace();
throw new Exception(e.getMessage());
}
catch (OperationApplicationException e)
{
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
private void updateDisplayPhoto(long rawContactId, byte[] photo)
{
Uri rawContactPhotoUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
try
{
AssetFileDescriptor fd = getContentResolver().openAssetFileDescriptor(rawContactPhotoUri, "rw");
OutputStream os = fd.createOutputStream();
os.write(photo);
os.close();
fd.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
Here is a tutorial to retrieve user's Google profile picture. It also retrieves some other things like account Email, name, ...
I think this is the straight way for your question :)
Stay in contact if it hepled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With