Android - Update a contact

前端 未结 3 392
我寻月下人不归
我寻月下人不归 2020-12-01 05:30

I\'m trying to update a contact of my phone book directly from my app. I\'m able to add and delete the contacts but the update just does nothing!

After the insert or

3条回答
  •  醉梦人生
    2020-12-01 06:10

    Finally, I found how to update a contact, here is the code of the update method:

        public void update()
        {       
            int id = 1;
            String firstname = "Contact's first name";
            String lastname = "Last name";
            String number = "000 000 000";
            String photo_uri = "android.resource://com.my.package/drawable/default_photo";
    
            ArrayList ops = new ArrayList();
    
            // Name
            Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
            builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
            builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastname);
            builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstname);
            ops.add(builder.build());
    
            // Number
            builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
            builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?"+ " AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)});
            builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
            ops.add(builder.build());
    
    
            // Picture
            try
            {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(photo_uri));
                ByteArrayOutputStream image = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
    
                builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
                builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
                builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
                ops.add(builder.build());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
            // Update
            try
            {
                getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    

    The field id is the raw contact id returned when you insert a new contact into the database. Here is the code to get this id:

        ContentProviderResult[] res;
        try
        {
            res = KramerApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            if (res != null && res[0] != null)
            {
                String uri = res[0].uri.getPath().substring(14);
                r.setBook_id( new Integer(uri).intValue() );
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    

    Check my first question if you want to know more about how to insert/delete a contact.

提交回复
热议问题