Display name not updating Email.Content_URI - android

I am trying to update Display name and email id of an existing contact, I am able to update email address, but display name not updating, the code I am using is here. I have tried plenty of more code but nothing is working for me please help.
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

Related

Android : Update exicting Contact List

I am trying to edit my contact list through my App. I can able to update Contact name , Phone number and Email. But, when I try to change existing photo it is not updating.
When I try to add new contact with image it is successfully added
Problem occur when I try to edit existing Contact with Image
Code that I using for Update Contact
ContentResolver contentResolver = getContentResolver();
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] emailParams = new String[]{idValue, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String[] nameParams = new String[]{idValue, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String[] numberParams = new String[]{idValue, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
int photoRow = -1;
String wherePhoto = ContactsContract.Data.RAW_CONTACT_ID + " = " + idValue + " AND " + ContactsContract.Data.MIMETYPE + " =='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, wherePhoto, null, null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if (cursor.moveToFirst()) {
photoRow = cursor.getInt(idIdx);
}
ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>();
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,emailParams)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, edt_contactEmail.getText().toString().trim())
.build());
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,nameParams)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, edt_contact_name.getText().toString().trim())
.build());
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,numberParams)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, edt_contactNumber.getText().toString().trim())
.build());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if(contact_bitmap!=null){ // If an image is selected successfully
contact_bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
byte[] b = stream.toByteArray();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data._ID + " = ?", new String[]{Integer.toString(photoRow)})
.withValue(ContactsContract.Data.RAW_CONTACT_ID, idValue)
.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.Data.DATA15, b)
.build());
try {
stream.flush();
}catch (IOException e) {
e.printStackTrace();
}
}
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(EditContacts.this,"Contact Successfully updated",Toast.LENGTH_LONG).show();
Intent i = new Intent(EditContacts.this,MainActivity.class);
finish();
startActivity(i);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
Can any one please tell me how to edit existing contact with image.
Thanks in advance :)
I have used this example for updating contact.
ContactManager
Method to update Contact:
boolean updateContact(String contactID, String contactName, String contactNumber, String contactEmailAdd, Bitmap bitmap) {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=?", new String[]{contactID, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE})
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contactName)
.build());
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=? AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?"
, new String[]{contactID, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)})
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contactNumber)
.build());
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
+ "=? AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?"
, new String[]{contactID, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE
, String.valueOf(Email.TYPE_WORK)})
.withValue(Email.ADDRESS, contactEmailAdd)
.build());
try {
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, image);
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " +
ContactsContract.Data.MIMETYPE + "=?", new String[]{contactID, Photo.CONTENT_ITEM_TYPE})
.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
.withValue(Photo.PHOTO, image.toByteArray())
.build());
/*Builder builder;
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?",
new String[]{contactID, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());*/
} catch (Exception e) {
e.printStackTrace();
}
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

Android custom fields are not showing up in Contacts

I am trying to update existing contact with custom fields of MIME type defined for my app. This gets added to Contact available in Contacts app but it's not visible there. I am not sure what I am missing, Please suggest if you find anything wrong with below code -
ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = ContentProviderOperation
.newInsert(RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME);
builder.withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE);
operationList.add(builder.build());
operationList
.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, contactId)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
"Display Name")
.build());
builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
// builder =
// ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
contactId);
builder.withValue(ContactsContract.Data.MIMETYPE, MIMETYPE);
builder.withValue(ContactsContract.Data.DATA1, contact.getId());
builder.withValue(ContactsContract.Data.DATA2, "Test Action");
builder.withValue(ContactsContract.Data.DATA3, "Test Action");
operationList.add(builder.build());
I am using same MIME type defined in contacts.xml file which is used for SyncAdapter.
Please help...
You can use below snippet to modify contact:
public void update()
{
int id = 1;
String firstname = "Contact's first name";
String lastname = "Last name";
String number = "000 000 000";
String photo_uri = "android.resource://com.my.package/drawable/default_photo";
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// Name
Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastname);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstname);
ops.add(builder.build());
// Number
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?"+ " AND " + ContactsContract.CommonDataKinds.Organization.TYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)});
builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
ops.add(builder.build());
// Picture
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(photo_uri));
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{String.valueOf(id), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());
}
catch (Exception e)
{
e.printStackTrace();
}
// Update
try
{
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
}

Why can not I set the email address if not set before the name to android?

I can not understand why I can not enter the email address unless you first insert the name , if the comment section what set the name the email works fine but I can not rightly then to enter the name , can anyone help ?
public String updateContact(String id, String name, String number,
String email, String surname, Context context,
ArrayList<EmailContactBean> listEmail) {
String resultId = null;
if (id == null) {
resultId = addContact(context, name, surname, number, email);
} else {
String raw_contact_id = getRawContactId(id, context);
ContentResolver contentResolver = context.getContentResolver();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// update name
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?",
new String[] { id })
.withValue(StructuredName.GIVEN_NAME, name).build());
// update surname
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(COLUMN_CONTACT_ID + "=? ",
new String[] { id })
.withValue(StructuredName.DATA3, surname).build());
// update number
Cursor numberCur = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { id }, null);
if ((null == numberCur) || (!numberCur.moveToFirst())) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID,
raw_contact_id)
.withValue(ContactsContract.Data.MIMETYPE,
Phone.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,
number).build());
} else {
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE
+ "=?",
new String[] { id, MIMETYPE_STRING_PHONE })
.withValue(COLUMN_NUMBER, number)
.build());
}
// update email
Cursor emailCur = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[] { id }, null);
if ((null == emailCur) || (!emailCur.moveToFirst())) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID,
raw_contact_id)
.withValue(ContactsContract.Data.MIMETYPE,
Email.CONTENT_ITEM_TYPE)
.withValue(Email.TYPE, listEmail.get(0).getType())
.withValue(ContactsContract.CommonDataKinds.Email.DATA,
listEmail.get(0).getName()).build());
} else {
for (int i = 0; i < listEmail.size(); i++) {
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data._ID + "=? AND "
+ COLUMN_MIMETYPE + "=?",
new String[] { listEmail.get(i).getId(),
MIMETYPE_STRING_EMAIL })
.withValue(COLUMN_EMAIL, listEmail.get(i).getName())
.withValue(COLUMN_EMAIL_TYPE,
listEmail.get(i).getType()).build());
}
}
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}
}
return resultId;
}
I solved it , just do not set MIMETYPE 's name and surname :
// update name
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID + "=? AND "
+ COLUMN_MIMETYPE + "=?",
new String[] { id, MIMETYPE_STRING_NAME })
.withValue(StructuredName.DATA2, name).build());
// update surname
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
COLUMN_CONTACT_ID + "=? AND " + COLUMN_MIMETYPE
+ "=?",
new String[] { id, MIMETYPE_STRING_NAME })
.withValue(StructuredName.DATA3, surname).build());

Insert a new android contact photo

I have a problem when I want to insert a photo for a contact.
I want to insert the "parent" contact image, which have no image.
I just want to change the photo of the contact, not the raw contact (contact_id)
It just works for Update, but not for insert, what is wrong ? The photo is not recorded in content providers.
//UPDATE
if (contact.getPhotoURL() != null){
ops.add(ContentProviderOperation
.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?",
new String[] { String.valueOf(contact.getId()), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE })
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray())
.build());
}else{
//INSERT
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValue(ContactsContract.Data.CONTACT_ID, contact.getId())
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray())
.build());
}
Thanks
EDIT :
this following code works
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
ContentResolver c = ctx.getContentResolver();
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + personId + " AND " + ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = c.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();
values.put(ContactsContract.Data.RAW_CONTACT_ID, personId);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray());
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoRow >= 0) {
c.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
} else {
c.insert(ContactsContract.Data.CONTENT_URI, values);
}
The field IS_SUPER_PRIMARY fixes the problem, the photo is saved for the "global" contact, not just the specific raw_contact.
Regards.

Modify contacts not working ( java.lang.NullPointerException )

I have a problem with editing contacts in Android. I'm using contentProviderOperation with newUpdate, but it's not working. It only works when the field already has something. If I have a field filled, erase it, then save and try to edit it again, I find that it's not saved and that's when I get the exception.
Here is the code I have to edit:
if (emailantigo.equals("") && !email.equals("")) {
debug("email " + email);
debug("id " + contactId);
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.RawContacts.CONTACT_ID, contactId)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
} else if (!email.equals(emailantigo)) {
debug("email " + email);
debug("id " + contactId);
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.RawContacts.CONTACT_ID
+ "=? AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE
+ "'",
new String[] { String.valueOf(contactId) })
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
}
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
public void debug(String msg) {
Log.i("CONTATOS", msg);
}
Here is the exception I get, from LogCat:
java.lang.NullPointerException
android.os.Parcel.readException(Parcel.java:1266)
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:137)
android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:460)
android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
android.content.ContentResolver.applyBatch(ContentResolver.java:622)
** EDIT 2 **
found solution by myself
the partial code that works:
Cursor cursorEmail = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "
+ contactId, null, null);
if (cursorEmail.moveToFirst()) {
if (!email.equals(emailantigo))
key_email = ops
.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID
+ "=? AND "
+ ContactsContract.Data.MIMETYPE
+ "=? AND "
+ ContactsContract.CommonDataKinds.Email.TYPE
+ "=?",
new String[] {
String.valueOf(contactId),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK) })
.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,
email).build());
}
cursorEmail.close();
if (!key_email && emailantigo.equals("") && !email.equals(emailantigo)) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID,
String.valueOf(contactId))
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
}
the newInsert not works with Data._ID or Data.Contact_ID, I dont know why, must have some logic behind this, but its working now ^^ !
Obs: Not tested in synced contacts.

Categories

Resources