我正在尝试更新现有联系人的显示名称和电子邮件id,我可以更新电子邮件地址,但显示名称不更新,我使用的代码在这里。我已经尝试了很多代码,但都不起作用,请帮帮我。
Cursor cursorEmail = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = "
+ model.getContactid(),
null, null);
if (cursorEmail
.moveToFirst()) {
ops.add(ContentProviderOperation
.newUpdate(
ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID
+ "=? AND "
+ ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {
String.valueOf(model
.getContactid()),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(
ContactsContract.CommonDataKinds.Email.ADDRESS,
txtEditedMailId
.getText()
.toString()
.trim()
.toLowerCase())
.withValue(
ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
txtEditedName
.getText()
.toString()
.trim()
.toLowerCase())
.build());
}
cursorEmail.close();
getContentResolver()
.applyBatch(
ContactsContract.AUTHORITY,
ops);
final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
Cursor cursorEmail = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = "
+ model.getContactid(),
null, null);
if (cursorEmail.moveToFirst()) {
//Update Email
ops.add(ContentProviderOperation
.newUpdate(Data.CONTENT_URI)
.withSelection(
Email.CONTACT_ID
+ "=? AND "
+ ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {
String.valueOf(model
.getContactid()),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(
ContactsContract.CommonDataKinds.Email.DATA,
txtEditedMailId
.getText()
.toString()
.trim()
.toLowerCase())
.build());
//Update image
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory
.decodeFile(localPathEditedImage,
options);
Logger.debug("path--------"
+ imgAbsPath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(
Bitmap.CompressFormat.PNG, 100,
baos); // bm is the bitmap object
byte[] photoByteArray = baos
.toByteArray();
Builder builder = ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI);
builder = ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(
ContactsContract.Data.CONTACT_ID
+ "=?"
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "=?",
new String[] {
String.valueOf(model
.getContactid()),
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE });
builder.withValue(
ContactsContract.CommonDataKinds.Photo.PHOTO,
photoByteArray);
ops.add(builder.build());
//Update Display name
ops.add(ContentProviderOperation
.newUpdate(
RawContacts.CONTENT_URI)
.withSelection(
Email.CONTACT_ID
+ " = ?",
new String[] { String.valueOf(model
.getContactid()) })
.withValue(
RawContacts.DISPLAY_NAME_PRIMARY,
txtEditedName.getText()
.toString())
.build());
}
cursorEmail.close();
//Execute Batch
getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops);
//Contact updated