How to update display name with email id?

I am trying to update the contact display name and email. After many efforts have achieved this from the following snipet code. But there is a problem with any name that I provide for the update that is added 2 after it when I see it in the contact application.

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"); 

This updates the display name as "priyanka 2" instead of "priyanka", as you saw, 2 is added after the display name.

+5
source share
1 answer

Finally, I got it here, there is my code there, thanks to everyone for your answers and support

  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 
+4
source

Source: https://habr.com/ru/post/1204421/


All Articles