How to add structured data to a new contact Intent

青春壹個敷衍的年華 提交于 2019-12-04 03:43:30
SelvaMariappan

Try this code it work for me

    String Streetname="ZZZZZ";
 ArrayList < ContentProviderOperation > ops = new ArrayList < ContentProviderOperation > ();
    ops.add(ContentProviderOperation.newInsert(
    ContactsContract.RawContacts.CONTENT_URI)
        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
        .build());
   ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
            .withValue(
        ContactsContract.CommonDataKinds.StructuredPostal.STREET,
        Streetname).build());

      try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) {
                  Log.e(getApplicationContext().toString(), "Exception: " + e.getMessage());
    } 

add to manifest

     <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
     <uses-permission android:name="android.permission.READ_CONTACTS"/>

more details see this post ..

How to add new contacts in android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!