Update contact pictures - support other providers

2019-07-24 19:38发布

问题:

The code at the bottom shows how I update contact pictures from my app. This works well, if the user uses sim, phone and google contacts and similar. But if he uses the outlook app, the outlook app does overwrite the images set by my app again after some time.

Can I somehow solve that? Can I force to overwrite the outlook image as well so that outlook syncs my new photo instead of their old one?

Code

byte[] photo = ImageUtil.convertImageToByteArray(bitmap, true);
ContentValues values = new ContentValues();
int photoId = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " == " +
        contact.getRawId() + " AND " + ContactsContract.Contacts.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()) {
    photoId = cursor.getInt(idIdx);
}
cursor.close();
values.put(ContactsContract.Data.RAW_CONTACT_ID, contact.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 (photoId >= 0) {
    MainApp.get().getContentResolver().update(
            ContactsContract.Data.CONTENT_URI,
            values,
            ContactsContract.Data._ID + " = " + photoId, null);
} else {
    MainApp.get().getContentResolver().insert(
            ContactsContract.Data.CONTENT_URI,
            values);
}

回答1:

Each SyncAdapters has a configuration called supportsUploading set to either true or false. You shouldn't modify RawContacts of accounts there were synced by a SyncAdapter with supportsUploading set to false, as most likely your change will probably get overwritten by the SyncAdapter soon after.

You can check the supportsUploading value of all SyncAdapters using the following code:

final SyncAdapterType[] syncs = ContentResolver.getSyncAdapterTypes();
for (SyncAdapterType sync : syncs) {
    Log.d(TAG, "found SyncAdapter: " + sync.accountType);
    if (ContactsContract.AUTHORITY.equals(sync.authority)) {
        Log.d(TAG, "SyncAdapter supports contacts: " + sync.accountType + " - supportsUploading=" + sync.supportsUploading());
    }
}

In order to set a different pic to a contact synced by a read-only SyncAdapter, you can create a new RawContact under your own account (preferably under your own SyncAdapter) and join that new RawContact with the existing RawContact created by Outlook, then you can set SUPER_PRIMARY on your own picture, so it'll be the default one.