无法以编程方式将安卓联系人插入安卓设备


ArrayList<ContentProviderOperation> ops =new ArrayList<ContentProviderOperation>();
         ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                  .withValue(Data.RAW_CONTACT_ID, 3)
                  .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                  .withValue(Phone.NUMBER, "999999999")
                  .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                  .withValue(Phone.DISPLAY_NAME, "hhhhhhh")
                  .withValue(ContactsContract.CommonDataKinds.Email.DATA, "abcd@gmail.com")
                  .build());
         ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                  .withValue(Data.RAW_CONTACT_ID, 4)
                  .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                  .withValue(Phone.NUMBER, "999999999")
                  .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                  .withValue(Phone.DISPLAY_NAME, "hhhlllllllllll")
                  .withValue(ContactsContract.CommonDataKinds.Email.DATA, "efgh@gmail.com")
                  .build());
         try {
             cr.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我添加了<uses-permission android:name="android.permission.WRITE_CONTACTS"/>权限。

我收到以下错误:

09-25 09:30:41.365: W/System.err(1057): android.content.OperationApplicationException: insert failed
09-25 09:30:41.375: W/System.err(1057):     at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:161)
09-25 09:30:41.375: W/System.err(1057):     at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:461)
09-25 09:30:41.375: W/System.err(1057):     at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:225)
09-25 09:30:41.375: W/System.err(1057):     at android.content.ContentResolver.applyBatch(ContentResolver.java:901)
09-25 09:30:41.375: W/System.err(1057):     at com.example.projdemo.MainActivity.onCreate(MainActivity.java:136)
09-25 09:30:41.375: W/System.err(1057):     at android.app.Activity.performCreate(Activity.java:5008)
09-25 09:30:41.385: W/System.err(1057):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-25 09:30:41.385: W/System.err(1057):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-25 09:30:41.395: W/System.err(1057):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 09:30:41.395: W/System.err(1057):     at android.os.Looper.loop(Looper.java:137)
09-25 09:30:41.395: W/System.err(1057):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-25 09:30:41.395: W/System.err(1057):     at java.lang.reflect.Method.invokeNative(Native Method)
09-25 09:30:41.395: W/System.err(1057):     at java.lang.reflect.Method.invoke(Method.java:511)
09-25 09:30:41.395: W/System.err(1057):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-25 09:30:41.395: W/System.err(1057):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-25 09:30:41.405: W/System.err(1057):     at dalvik.system.NativeStart.main(Native Method)

尝试如下:

private void addContact() {
    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, "Second Name") 
            .withValue(StructuredName.FAMILY_NAME, "First 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, "09876543210")
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_HOME)
            .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, "abc@xyz.com")
            .withValue(ContactsContract.CommonDataKinds.Email.TYPE, Email.TYPE_WORK)
            .build());
    try{ 
        ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list); 
    }catch(Exception e){ 
        e.printStackTrace(); 
    } 
}
嗨,

我知道有点晚了,但它可能会帮助你。首先,您必须在清单文件中注册使用权限,如下所示:

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

然后,您可以尝试使用以下方法:

private void addContact(String name, String phone) {
        ContentValues values = new ContentValues();
        values.put(Contacts.People.NUMBER, phone);
        values.put(Contacts.People.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
        values.put(Contacts.People.LABEL, name);
        values.put(Contacts.People.NAME, name);
        Uri dataUri = getContentResolver().insert(Contacts.People.CONTENT_URI, values);
        Uri updateUri = Uri.withAppendedPath(dataUri, Contacts.People.Phones.CONTENT_DIRECTORY);
        values.clear();
        values.put(Contacts.People.Phones.TYPE, Contacts.People.TYPE_MOBILE);
        values.put(Contacts.People.NUMBER, phone);
        updateUri = getContentResolver().insert(updateUri, values);
        Log.d("CONTACT", ""+updateUri);
    }

最新更新