Behrooz Amirinejad
Behrooz Amirinejad

Reputation: 31

android add custom field to all contacts

I'm trying to add my application icon to all existing contacts . my intent is to get contact by tapping user on my application icon to open app and send this contact to cloud. i could create contact by application and add my app MimeType to this and get save contact but i want to update all existing contact with my app icon this is my code that inserts my app mimeType to all contacts but it doesn`t work

Cursor cursor =  context.getContentResolver().query(Data.CONTENT_URI, new       String[] {       Data.RAW_CONTACT_ID,       Data.DISPLAY_NAME, Data.MIMETYPE,    Data.CONTACT_ID }, 
     null, 
     null, 
     null);




 ArrayList<ContentProviderOperation> ops = new  ArrayList<ContentProviderOperation>();
    do {// get id of contact and update all contact
          id = cursor.getInt(0);
          ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValue(ContactsContract.Data.RAW_CONTACT_ID, id)
         .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE)
         .withValue(Data.DATA1, "profile1") 
         .withValue(Data.DATA2, "profile2") 
         .withValue(Data.DATA3, "profile3") 
         .build()); 
          try { 
    context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

       } catch (Exception e1) {
        e1.printStackTrace();
       Log.i("Error =>>" + e1.getMessage());
       }
   while(cursor.moveNext())

and please help me!thanks

Upvotes: 3

Views: 879

Answers (1)

Nynuzoud
Nynuzoud

Reputation: 121

You can check this link. There's helpful example. https://github.com/nemezis/SampleContacts/tree/master/src/com/nemezis/sample/contacts

Upvotes: 1

Related Questions