8
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)
4

2 に答える 2

38

次のようにしてみてください:

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(); 
    } 
}
于 2012-09-25T04:37:38.570 に答える
1

こんにちは私は少し遅いことを知っていますが、それはあなたを助けるかもしれません。まず、次のようにマニフェストファイルに使用許可を登録する必要があります。

<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);
    }
于 2016-01-27T08:23:11.050 に答える