0

I'm developing app to provide and set pictures for contacts from social networks, and I'm already figure out how to set picture to contact, but when I do this programmatically picture is stored in lower quality. For example here is 2 examples, first picture is set with my app, second with native android contacts app (source picture is exactly the same for both cases):

First example

With my app:

With my app

With native app:

With native app

Second example

With my app:

With my app

With native app:

With native app

You can see pixilization on the hands and other parts.

The code I've used to set contact picture:

public static boolean setContactPhoto(long contactId, byte[] photo) {
    ContentResolver cr = context.getContentResolver();
    ContentValues values = new ContentValues();
    long photoId = -1;
    long rawContactId = -1;
    Cursor rawContactsCursor = cr.query(
            ContactsContract.RawContacts.CONTENT_URI,
            new String[]{ContactsContract.RawContacts._ID},
            String.format("%s=%d", ContactsContract.RawContacts.CONTACT_ID, contactId),
            null,
            null
    );
    while (rawContactsCursor.moveToNext()) {
        rawContactId = rawContactsCursor.getLong(rawContactsCursor.getColumnIndex(ContactsContract.RawContacts._ID));
        String where = String.format(
                "%s=%d AND %s=='%s'",
                ContactsContract.Data.RAW_CONTACT_ID,
                rawContactId,
                ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
        );
        Cursor dataCursor = cr.query(
                ContactsContract.Data.CONTENT_URI,
                new String[]{ContactsContract.Data._ID},
                where,
                null,
                null
        );
        if (dataCursor.moveToFirst()) {
            photoId = dataCursor.getLong(dataCursor.getColumnIndex(ContactsContract.Data._ID));
            dataCursor.close();
            break;
        }
        dataCursor.close();
    }
    rawContactsCursor.close();

    if (rawContactId < 0) return false;

    values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
    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) {
        return cr.insert(ContactsContract.Data.CONTENT_URI, values) != null;
    } else {
        return cr.update(ContactsContract.Data.CONTENT_URI, values, String.format("%s=%d", ContactsContract.Data._ID, photoId), null) == 1;
    }
}

My phone has 540x960 resolution, so source pictures are 960x960, this is strange, but if I try to set 540x960 picture it is cropped from all sides and scaled up, so pixilization is bigger.

So, how to avoid such pixilization setting up contact picture programmatically?

Community
  • 1
  • 1
Luft-on
  • 179
  • 1
  • 13

1 Answers1

0

As described in the documentation, the .photo field hold the "Thumbnail photo of the raw contact."

I think you have to set the PHOTO_FILE_ID used to "accessing full-size photos by photo file ID".

See the related ContactsContract.DisplayPhoto

BrainCrash
  • 12,992
  • 3
  • 32
  • 38
  • Am I right, I have to write bitmap data to temp directory and set PHOTO_FILE_ID and then delete temp bitmap file? Can you give some code snippet? – Luft-on Mar 08 '14 at 16:55