我正在尝试更新联系人显示名称&电子邮件。经过多次努力,从以下代码snipet实现了这一点。但是当我在联系人应用程序中看到它时,有一个问题我提供更新后附加2。
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation
.newUpdate(
ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?",
new String[] { String.valueOf(model
.getContactid()) })
.withValue(ContactsContract.Data.MIMETYPE,
Email.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Email.DATA,
"priyankay27@gmail.com")
.withValue(
ContactsContract.CommonDataKinds.Email.DISPLAY_NAME,
"priyanka")
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Context ctx = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(ctx,"Update successful", duration);
toast.show();
Logger.debug("Update successful");
这会将显示名称更新为&#34; priyanka 2&#34;而不是&#34; priyanka&#34;如你所见2,在显示名称后附加。
答案 0 :(得分:4)
最后我在这里运行了我的代码,感谢您的回复和支持
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