Update contact image in android contact provider

2019-02-24 04:18发布

问题:

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 Application without image. then we can't update contact Image. My Updating Code is.

    ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
                .withSelection(Data.CONTACT_ID+"= ? AND "+ContactsContract.Data.MIMETYPE+"=?",new String[]{id,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE})
                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, imageInByte)
                .build());

Please provide Solution Regard this.

回答1:

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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
                    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