8

I am trying to add a contact in Android using getContentResolver. First I created an ArrayList:

ArrayList<ContentProviderOperation> ops =
  new ArrayList<ContentProviderOperation>();

then populated the array list by

int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(ContactsContract.RawContacts.ACCOUNT_NAME,accountName)
   .build());

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, name)
   .build());

and finally in a try block

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

when I excecute this, I am not getting any error or exception. But the contact does not appear in the Android contacts. When I retrieve the invisible contacts I could find this contact. Can any one figure out what is going wrong?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Ads
  • 6,681
  • 12
  • 47
  • 72
  • 1
    Probably dumb question but on the phone setting do you have make visible only contact with numbers. – Terrance Dec 08 '10 at 15:26
  • 1
    Another thing I'd like to add is that some phones allow you to select to show only the contacts that belong to certain Google groups (whichever you setup). If a new contact is not part of one of those groups you won't see him. Check your GMail account and see if the contact was correctly synced back up there. – Jason L. Dec 08 '10 at 15:47
  • @Terrance:may i know whether u know the answer or not? there is nothing to do with making contact visible..even in my app if i want to retrieve these contacts,i can only use invisible flag. – Ads Dec 08 '10 at 15:51
  • @Jason:i don't any any group in my emulator, but still i can add and view the contact.how is it possible? – Ads Dec 08 '10 at 15:53
  • @Adhavan This would'nt be in your app, it would be under contacts->menu->Display Options-> Only Contacts with phones. Like I said may be stupid question here but I was curious as to what the answer was and if selecting it made any difference. If I was absolutely sure of the answer I would have posted a formal answer. – Terrance Dec 08 '10 at 16:04
  • @Terrance:ha ha i know there is visible and invisible contact.first,do u know that we can retrieve our in-built contacts to our app?you are not here to give stupid answer and comment like this.secondly,u know where contact db is? when i checked in that db,all the contact added from my app gets invisible flag as trueand native contact app gets flag value as false.i hope u can learn the contact api and come back! – Ads Dec 08 '10 at 17:42
  • 1
    Dude whats with all the hate. I wasn't trying to be an ass or anything. I was just curious about that simple thing that you got seemed to have gotten upset about. I told I didnt have the answer. You said there is nothing to do with making contacts visible. I misunderstood what you said there as to mean that there is no way to do that. After all there is no such thing as a stupid question and seeing how you said there were no obvious errors thrown and when your invisible contact ArrayList was retrieved so id sounded like a simple explanation to the problem. – Terrance Dec 08 '10 at 17:53
  • Contact shows up on invisible and doesn't on the regular list HMMMMMMM Maybe its because he is invisible and you have "Only contacts with phones" checked in your emulator. I'd say its a perfectly reasonable question to ask. – Terrance Dec 08 '10 at 17:55
  • Maybe its because he is invisible and you have "Only contacts with phones" checked in your emulator--i am sure your are not an android developer! no use in arguing with u – Ads Dec 08 '10 at 20:38
  • @Terrance did you flag that comment or did somebody else? Also, @Adhavan its usually considered bad form to be rude to people trying to help you out. Also, T, remember what I said about android? Yes. Quite. –  Dec 09 '10 at 17:23
  • @Will: i was not rude.as i didn't know the answer i post this question...how is he to say that my question is dumb question or stupid question? – Ads Dec 10 '10 at 06:04
  • 3
    @Adhavan LOL I see the problem! @Terrance was saying that the question HE was asking YOU might be considered "dumb." He wasn't saying that *your* question was dumb! He was being humble, not rude. –  Dec 10 '10 at 14:53
  • Yeah sorry about the miss communication. I meant you no offense. – Terrance Dec 11 '10 at 03:48
  • 1
    @Will,@Terrance: Sorry! i think i misunderstood! – Ads Dec 13 '10 at 08:38

1 Answers1

10
 ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>();
 op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
      //.withValue(RawContacts.AGGREGATION_MODE, RawContacts.AGGREGATION_MODE_DEFAULT)
        .build()); 

     // first and last names
 op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.GIVEN_NAME, name)
        .withValue(StructuredName.FAMILY_NAME, name)
        .build());

 op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,  Phone.TYPE_MOBILE)
        .build());

  op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, 0)
       .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
         .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
         .build());

try{
     ContentProviderResult[] results = getContentResolver().
                                 applyBatch(ContactsContract.AUTHORITY, op_list);
}catch(Exception e){
     e.printStackTrace();
} 

this code works!

Ali Imran
  • 8,927
  • 3
  • 39
  • 50
Ads
  • 6,681
  • 12
  • 47
  • 72
  • this doesn't work. null as an account type and name doesn't work on some phones. – Creniale Oct 25 '11 at 02:05
  • 1
    For every phone to work, you will need to have an account type and account name. One possible way is to let user choose which account they want to add the contacts to. (save the choice as a resource and re-use it) – Creniale Nov 22 '11 at 10:03
  • I am able to insert the contact. But the problem in setting contact ringtone. How can i achieve this. – sachi Sep 25 '12 at 10:05
  • @Ads Good Work, Only this code worked for me after long RND. – Farhana Naaz Ansari Apr 03 '18 at 07:06