2

I am having a hard time understanding how to get information from a contact and also how to put information to a contact. Does anyone know of a good way to learn?

Anyways, to my main question. I got the contact image from some random code I found on the internet but now I want to set a new Bitmap image to my contact. What information do I need for the contact (URI or Contact_id) and it would be awesome if someone could give me a very basic and simple simple of code and kind of step though it. But if you don't have the time to step through it then just a basic code simple should be good. Thanks for any help.

EDIT GOT WORKING CODE NOW: This is the code that I used:

// Update picture to contact
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        //contactUriId is my contact URI with just contact id number at the end of the "content://..."
        try
        {
            Bitmap bitmap = bmpNewContact;
            ByteArrayOutputStream image = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);

            Builder contentOp = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
            contentOp.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(contactUriId), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
            contentOp.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
            ops.add(contentOp.build());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        // Update
        try
        {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
BinaryNexus
  • 875
  • 3
  • 15
  • 30
  • Check if this post has some valuable points for you. http://stackoverflow.com/questions/2200436/update-contact-details-on-android – Niko Apr 11 '12 at 05:23
  • I was kind of wanting something more up to day because isnt the People.{whatever} depreciated? If I'm going to do this, I would like to do it right. But thank you for the suggestion. Does anyone else have a suggestion? – BinaryNexus Apr 11 '12 at 14:55

1 Answers1

0
// Update picture to contact
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    //contactUriId is my contact URI with just contact id number at the end of the "content://..."
    try
    {
        Bitmap bitmap = bmpNewContact;
        ByteArrayOutputStream image = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);

        Builder contentOp = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
        contentOp.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(contactUriId), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
        contentOp.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
        ops.add(contentOp.build());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    // Update
    try
    {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
BinaryNexus
  • 875
  • 3
  • 15
  • 30