Update contact image in android contact provider

后端 未结 1 932
庸人自扰
庸人自扰 2021-01-19 18:06

I create an Application to Read, Update, Delete Contacts Details. Here is a problem to updating Contact_Image.

When new contact Added by device outside the Applicati

1条回答
  •  醉话见心
    2021-01-19 18:43

    You will have different code for updating a photo then adding a photo to a contact that doesn't have one. From your description above I believe you are trying to insert an image and not update an image, but here is code for both:

            if(hasPhoto(resolver, id) == true)
            {
                int photoRow = -1;
                String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + id + " AND " + ContactsContract.Data.MIMETYPE + " =='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; 
                Cursor cursor = resolver.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
                int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
                if (cursor.moveToFirst()) {
                    photoRow = cursor.getInt(idIdx);
                }
                cursor.close();
    
                // Update current photo
                ArrayList ops = new ArrayList();
                ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(ContactsContract.Data._ID + " = ?", new String[] {Integer.toString(photoRow)})
                        .withValue(ContactsContract.Data.RAW_CONTACT_ID, id)
                        .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.Data.DATA15, photoBytes)
                        .build());
    
                try {
                    resolver.applyBatch(ContactsContract.AUTHORITY, ops);
                } catch (RemoteException e) {
    
                } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else
            {
                // Create new photo entry
                int rawContactId = -1;
                Cursor cursor = resolver.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.RawContacts.CONTACT_ID + "=?", new String[] {id}, null);
    
                if(cursor.moveToFirst())
                {
                    rawContactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
    
                    if(rawContactId > -1)
                    {
                        ArrayList ops = new ArrayList();
                        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                                .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photoBytes)
                                .build());
    
                        try 
                        {
                            resolver.applyBatch(ContactsContract.AUTHORITY, ops);
                        } catch (RemoteException e) {
    
                        } catch (OperationApplicationException e) {
                        // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
    

    The difference being that if you are updating an existing photo you use the newUpdate function, but if you are inserting a photo to a contact that never had one you use newInsert

    0 讨论(0)
提交回复
热议问题