Edit and update contact's data in android - android

i made an application that have read/write contacts permissions. i'm trying to make an updating function that gets new display name, an email address and phone number to add by using this:
public void updateContact(Context ctx, String name, String number, String email,String ContactId) {
try {
name = name.trim();
email = email.trim();
number = number.trim();
ContentResolver contentResolver = ctx.getContentResolver();
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] emailParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
String[] nameParams = new String[]{ContactId, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
String[] numberParams = new String[]{ContactId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
ArrayList<android.content.ContentProviderOperation> ops = new ArrayList<android.content.ContentProviderOperation>();
if(!email.equals(""))
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,emailParams)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
.build());
}
if(!name.equals(""))
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,nameParams)
.withValue(ContactsContract.Contacts.DISPLAY_NAME, name)
.build());
}
if(!number.equals(""))
{
ops.add(android.content.ContentProviderOperation.newUpdate(android.provider.ContactsContract.Data.CONTENT_URI)
.withSelection(where,numberParams)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
.build());
}
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
}
Sadly this code isn't making any changes to the contact and it's hard for me to find out why.
Thanks for any help :)

well, nobody helps me but i managed it by my own.
for anyone who gets the same issues i'm posting the answer for my own question.
the following code will update the phone, name and email's contact.
public static void updateContact(Context ctx, String name, String number, String email, Long id) {
try {
ContentValues contentValues;
long ret = id;
Log.i("contact id", ret + "");
if (!name.equals("")) {
contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, ret);
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
ctx.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, contentValues);
}
if (!number.equals("")) {
contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, ret);
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, number);
int phoneContactType = ContactsContract.CommonDataKinds.Phone.TYPE_HOME;
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, phoneContactType);
ctx.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, contentValues);
}
if (!email.equals("")) {
contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, ret);
contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM);
ctx.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, contentValues);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Related

Performance of update existing contact in Android so slow

i have a problem in update existing contact in Android. My code execute about 400 contact but it take me about 20s to update it. Here is my code.
public class Contact {
private long contactId;
private String phoneNumber;
private int phoneType;
}
Then i try to update each contact filter by ID and Type:
ArrayList list = arrayLists[0];
for (int i = 0; i < list.size(); i++) {
Contact contact = list.get(i);
long contactID = contact.getContactId();
String newPhoneNumber = contact.getPhoneNumber();
int phoneType = contact.getPhoneType();
updateContact(contactID, newPhoneNumber, phoneType);
}
Here is my updateContact funtion:
private void updateContact(long contactID, String newPhoneNumber, int phoneType) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " + ContactsContract.CommonDataKinds.Phone.TYPE + " = ?";
String[] whereParams = new String[]{contactID + "", phoneType + ""};
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, newPhoneNumber);
Uri dataUri = ContactsContract.Data.CONTENT_URI;
cr.update(dataUri, contentValues, where, whereParams);
}
I tried other method to update contact but it also take same time ( about 20s for 400 contact)
private void updateContact(long contactID, String newPhoneNumber, int phoneType) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = ?";
String[] params = new String[]{contactID + "", phoneType + ""};
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newPhoneNumber)
.build());
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
What should i do, how can i improve the performance of update job. I expert it can update 400 contact in 3-5s

trying to update a contact with this code isn't working

I'm trying to use this code to allow a user edit a contact's name and number. I get no errors and when I print nameofcontact and numberofcontact in my log it shows me the latest changes I've made to the name and number of the contact.
But it's not saving to my contacts database. Any ideas what's wrong?
public void editButton(View view) {
// the text in the 'nameofcontact' edittext box, can be modified by the user
contactname = nameofcontact.getText().toString();
// the text in the 'numberofcontact' edittext box, can be modified by the user
contactnumber = numberofcontact.getText().toString();
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.DISPLAY_NAME + " = ? AND " +
ContactsContract.Data.MIMETYPE + " = ? AND " +
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE) + " = ? ";
String[] params = new String[] {contactname,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_HOME)};
Cursor phoneCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// if ( (null == phoneCur) ) {
// createContact(name, phone);
// } else
{
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.CommonDataKinds.Phone.DATA, contactnumber)
.build());
}
phoneCur.close();
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println (contactname);
System.out.println (contactnumber);
Toast.makeText(this, "Updated", Toast.LENGTH_SHORT).show();
}
Please check you add below permission in your manifeast or not
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
and you can use this method it works for me.
public boolean updateContact(String name, String number, String ContactId) {
boolean success = true;
String phnumexp = "^[0-9]*$";
try {
name = name.trim();
number = number.trim();
if (name.equals("") && number.equals("")) {
success = false;
} else if ((!number.equals("")) && (!match(number, phnumexp))) {
success = false;
} else {
ContentResolver contentResolver = SwipableHomeActivity.this
.getContentResolver();
String where = Data.CONTACT_ID + " = ? AND "
+ Data.MIMETYPE + " = ?";
String[] nameParams = new String[]{
ContactId,
StructuredName.CONTENT_ITEM_TYPE};
String[] numberParams = new String[]{
ContactId,
Phone.CONTENT_ITEM_TYPE};
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
if (!name.equals("")) {
ops.add(ContentProviderOperation
.newUpdate(
Data.CONTENT_URI)
.withSelection(where, nameParams)
.withValue(StructuredName.DISPLAY_NAME, name)
.build());
}
if (!number.equals("")) {
ops.add(ContentProviderOperation
.newUpdate(
Data.CONTENT_URI)
.withSelection(where, numberParams)
.withValue(Phone.NUMBER, number).build());
}
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
}
} catch (Exception e) {
e.printStackTrace();
success = false;
}
return success;
}

How to set default image to Android phone contact that has no previous image

I have a piece of code which updates the an Android contact´s image, the problem is that it doesn't work when the contact has no previous image. I also checked that the contact was from "Phone" account or "*#gmail.com" account. When it already has an image with these accounts I have no problem updating the image, the problem is just when the contact has no previous image assigned.
Here is the method in charge of updating the image.
public void update(long id, Bitmap bitmap) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
// Picture
try
{
ByteArrayOutputStream image = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
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(id), ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});
builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
ops.add(builder.build());
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
this.context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e)
{
e.printStackTrace();
}
}
And here is how I know if the contact belongs to one of the two accounts I mentioned above:
// Getting the raw contact id
public static int getRawContactId(Context context, long id) {
String[] projection = new String[] { ContactsContract.RawContacts._ID };
String selection = ContactsContract.RawContacts.CONTACT_ID + "=?";
String[] selectionArgs = new String[] { String.valueOf(id) };
Cursor c = context.getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI, projection,
selection, selectionArgs, null);
int rawContactId = -1;
if (c.moveToFirst()) {
rawContactId = c.getInt(c
.getColumnIndex(ContactsContract.RawContacts._ID));
}
return rawContactId;
}
Here I get the account name for further analysis
//...
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
String[] rawProjection = { RawContacts._ID, RawContacts.ACCOUNT_NAME };
Cursor raw = context.getContentResolver().query(rawContactUri, rawProjection, null, null, null);
if (raw.moveToFirst()) {
account = raw.getString(1);
}
Thanks in advance.
The problem seems to be that, as you describe, you're updating the image
http://developer.android.com/reference/android/content/ContentProviderOperation.html#newUpdate(android.net.Uri)
instead of inserting a new one
http://developer.android.com/reference/android/content/ContentProviderOperation.html#newInsert(android.net.Uri)
If the contact doesn't has a previous image, it's impossible to update the field in the database because it doesn't exists. You should perform an insert operation instead.
It's debatable if the api method should fail hard throwing an exception at runtime.
Hope it helps.
After some research as suggested I found an approach that works just fine.
https://stackoverflow.com/a/15145256/2423274
Here is the relevant part:
// Create new photo entry
int rawContactId = -1;
Cursor cursor = resolver.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.RawContacts.CONTACT_ID + "=?", new String[] {id}, null);
if(cursor.moveToFirst()) {
rawContactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.RawContacts._ID));
if(rawContactId > -1) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photoBytes)
.build());
try {
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e) {
e.printStackTrace();
}

Update information of an contact in android

I want to update new information of an contact. I need to update 3 fields: Name, phone number and company name of the contact. Here is my code. The problem of my is: the method does not update any new information!
Could you show me the reasons why I have the error. And show me how to fix it! Thanks guys!
public void editContact(String _id, String name, String phone, String company) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data._ID + " = ?" ;
String[] params = new String[] {_id};
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(where, params)
.withValue(ContactsContract.Data.DISPLAY_NAME, name)
.withValue(ContactsContract.CommonDataKinds.Phone.DATA, phone)
.withValue(ContactsContract.CommonDataKinds.Organization.DATA, company)
.build());
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
Log.e("ERROR UPDATE: ", e.getMessage());
}
this.finish();
Toast.makeText(getApplicationContext(), "Contact saved", Toast.LENGTH_SHORT).show();
this.callHomeActivity();
}
#Stacks28: This is me - Mr.Pakapun
I try to update only the name of the contact by using the code below. BUT, nothing happends! The name of the contact is not been updated. What wrong with the code ?
public void editContact(String _id, String name, String phone, String company) {
String where = ContactsContract.Data._ID + " = ? AND " +
ContactsContract.Data.MIMETYPE + "= ?";
String[] params = new String[] {_id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
ContentResolver cr = getContentResolver();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(where, params)
.withValue(StructuredName.DISPLAY_NAME, name)
.build());
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
Log.e("ERROR UPDATE: ", e.getMessage());
}
}

How to Add Contacts to Contacts Table

I am trying to insert a contact through my application but i am not able to figure out what should be the value of accountType and accountName as below.
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,"1-800-111-411");
getContentResolver().insert(Data.CONTENT_URI, values);
Also when i try to execute this code with the following changes in the accountType and accountName, i am unable to see it in the Contacts.
values.put(RawContacts.ACCOUNT_TYPE, "acc_type");
values.put(RawContacts.ACCOUNT_NAME, "acc_name");
But it seems that some values get inserted as when i search for "Mike Sullivan" i get the contact but without the Phone Number.
Please Help
below is the code to add contact database and also it return whether the contact was added or not::::
//to save contact in Database
public boolean SaveContact(Activity _activity,String name,String number) {
String MIMETYPE_RADUTOKEN = "vnd.android.cursor.item/radutoken";
String szname = name,szMobile = number;
//Create a new contact entry!
String szToken = String.format("RADU_TOKEN_%d", System.currentTimeMillis());
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(RawContacts.ACCOUNTTYPE, null).withValue(RawContacts.ACCOUNT_NAME, null).build());
//INSERT NAME
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, szname).build());
//INSERT PINLESSMAX MOBILE NUMBER
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, szMobile).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM).withValue(ContactsContract.Data.DATA3, "PinLessMax").build());
// SAVE CONTACT IN BCR Structure
Uri newContactUri = null;
//PUSH EVERYTHING TO CONTACTS
try{
ContentProviderResult[] res = _activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
if (res!=null && res[0]!=null) {
newContactUri = res[0].uri;
}
}catch (RemoteException e) {
// error
newContactUri = null;
} catch (OperationApplicationException e) {
// error
newContactUri = null;
}
if (newContactUri == null) {
return false;
}
boolean foundToken = false;
// IDENTIFY Contact based on name and token
String szLookupKey = "";
Uri lkup = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, szname);
ContentResolver cr = _activity.getContentResolver();
Cursor idCursor = _activity.getContentResolver().query(lkup, null, null, null, null);
// get all the names
while (idCursor.moveToNext()) {
String szId = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts._ID));
String szName = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
szLookupKey = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
// for this contact ID, search the custom field
String tokenWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] tokenWhereParams = new String[]{szId, MIMETYPE_RADUTOKEN};
Cursor tokenCur = cr.query(ContactsContract.Data.CONTENT_URI, null, tokenWhere, tokenWhereParams, null);
while (tokenCur.moveToNext()) {
String token = tokenCur.getString(tokenCur.getColumnIndex(ContactsContract.Data.DATA1));
// CHECK THE TOKEN!
if (szToken.compareTo(token) == 0) {
tokenCur.close();
foundToken = true;
break;
}
}
tokenCur.close();
if (foundToken) break;
}
idCursor.close();
return true;
}//SaveContact()
You can give null values for account type and account name if you do not want to create the contact under a specific account
values.put(RawContacts.ACCOUNT_TYPE, null);
values.put(RawContacts.ACCOUNT_NAME, null);
In most of the devices it creates as a default phone contact.
If you want to know all the account types available in the device,
You can use the following code
Account[] accountList = AccountManager.get(this).getAccounts();
for(int i = 0 ; i < accountList.length ; i++) {
System.out.println(accountList[i].type);
}
Note: Different OEMs uses different name for account type.

Categories

Resources