Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update display name with email id?

I am trying to update a contact display name & email.After lot of efforts achieve that from following code snipet.But there is a issue any name I supply for update it appended 2 after it when I see it in contact app.

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,
                "[email protected]")
        .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 display name as "priyanka 2" instead of "priyanka" as you seen 2 get appended after display name.

like image 731
PRIYANKA YADAV Avatar asked Sep 30 '22 16:09

PRIYANKA YADAV


1 Answers

Finally I got it run here there is my code, thanks all for your replies 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
like image 172
Mohd Mufiz Avatar answered Oct 03 '22 01:10

Mohd Mufiz