我正在尝试使用以下代码以编程方式添加联系人 -
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();
}
}
但是我得到了例外。这是我的堆栈跟踪 -
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)
我不确定为什么我在下一行中插入失败 -
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
任何提示?
答案 0 :(得分:0)
我注意到您那里应该有0,而应该改为rawContactID
。
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());
我实际上已经可以使用它了,您可以在这里查看我的要旨。
https://gist.github.com/delacrixmorgan/3b3efef4e449a9366836158c221142cd