android – OperationApplicationException on adding contact-ThrowExceptions

Exception or error:

I am trying to add a contact programmatically into my contact phonebook using ContentProviderOperation.

I have already enabled WRITE_CONTACT permission in my AndroidManifest.xml.

Here are the following code.

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

int rawContactID = ops.size();
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.StructuredName.CONTENT_ITEM_TYPE)
 .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, etname.getText().toString())
 .build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
 .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
 .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, etmob.getText().toString())
 .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
 .build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
 .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
 .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, etphone.getText().toString())
 .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
 .build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
 .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE)
 .withValue(ContactsContract.CommonDataKinds.SipAddress.DISPLAY_NAME, etadd.getText().toString())
 .build());

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
 .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
 .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, etmail.getText().toString())
 .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
 .build());

try {
 getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
 Toast.makeText(getBaseContext(), "Contact is successfully added", Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
 e.printStackTrace();
} catch (OperationApplicationException e) {
 e.printStackTrace();
}

but I am getting the exception. Here’s my stack trace –

07-10 09:32:31.379    1143-1143/com.example.addcontact.app W/System.err﹕ android.content.OperationApplicationException: insert failed
07-10 09:32:31.379    1143-1143/com.example.addcontact.app W/System.err﹕ at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:158)
07-10 09:32:31.379    1143-1143/com.example.addcontact.app W/System.err﹕ at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:512)
07-10 09:32:31.379    1143-1143/com.example.addcontact.app W/System.err﹕ at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:377)
07-10 09:32:31.379    1143-1143/com.example.addcontact.app W/System.err﹕ at android.content.ContentResolver.applyBatch(ContentResolver.java:1227)
07-10 09:32:31.389    1143-1143/com.example.addcontact.app W/System.err﹕ at com.example.addcontact.app.MainActivity$1.onClick(MainActivity.java:85)
07-10 09:32:31.389    1143-1143/com.example.addcontact.app W/System.err﹕ at android.view.View.performClick(View.java:4438)
07-10 09:32:31.389    1143-1143/com.example.addcontact.app W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
07-10 09:32:31.389    1143-1143/com.example.addcontact.app W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
07-10 09:32:31.389    1143-1143/com.example.addcontact.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-10 09:32:31.389    1143-1143/com.example.addcontact.app W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
07-10 09:32:31.389    1143-1143/com.example.addcontact.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
07-10 09:32:31.399    1143-1143/com.example.addcontact.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-10 09:32:31.399    1143-1143/com.example.addcontact.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
07-10 09:32:31.399    1143-1143/com.example.addcontact.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-10 09:32:31.409    1143-1143/com.example.addcontact.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-10 09:32:31.409    1143-1143/com.example.addcontact.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

I am not sure why I am getting insert failed at the following line.

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

Any hints?

How to solve:

I’ve noticed that you have 0 there, when it should be rawContactID instead.

ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, etname.getText().toString())
                        .build());

I’ve actually got it to work, you can checkout my gist here.

https://gist.github.com/delacrixmorgan/3b3efef4e449a9366836158c221142cd

Leave a Reply

Your email address will not be published. Required fields are marked *