添加联系人到Android组

时间:2013-12-03 07:37:20

标签: android contacts android-contentprovider

当我添加联系人而没有将他添加到分组时它的工作但是什么时候 我尝试添加到特定组(我有id)它是faild。 我读了很多答案如何添加联系人到组。但它一直都是假的。

这是我的代码:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
             .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "meu")
             .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "meu")
             .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, contact.name)
             .build());
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone._ID, contact.index)
             .build());
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone)
             .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
             .build());
     if(contact.phone2 !=" "){
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone2)
             .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
             .build());
     }
     if(contact.phone3 !=" "){
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone3)
             .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
             .build());
     }
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.fax)
             .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_FAX_WORK)
             .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,contact.street+" "+contact.streetNumber+","+contact.city)
             .build());
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,Screen.currentContext.getResources().getString(R.string.meuhedet))
             .build());

    ops.add(ContentProviderOperation.newInsert(ContactsContract.Groups.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 10)
             .build());

     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     Bitmap photo = BitmapFactory.decodeResource(Screen.getContext().getResources(),R.drawable.logo);
     photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
         .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray())
         .build());

我在Log中找到了这条踪迹:

  

12-03 10:17:11.693:E / SQLiteLog(18436):( 1)表组没有名为mimetype的列   12-03 10:17:11.698:E / SQLiteDatabase(18436):插入mimetype = vnd.android.cursor.item / group_membership account_id = 1 data1 = 10 dirty = 1 raw_contact_id = 2时出错   12-03 10:17:11.698:E / SQLiteDatabase(18436):android.database.sqlite.SQLiteException:表组没有名为mimetype的列(代码1):,编译时:INSERT INTO groups(mimetype,account_id,data1, dirty,raw_contact_id)VALUES(?,?,?,?,?)   12-03 10:17:11.698:E / SQLiteDatabase(18436):at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)   12-03 10:17:11.698:E / SQLiteDatabase(18436):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1012)   12-03 10:17:11.698:E / SQLiteDatabase(18436):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)   12-03 10:17:11.698:E / SQLiteDatabase(18436):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)   12-03 10:17:11.698:E / SQLiteDatabase(18436):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)   12-03 10:17:11.698:E / SQLiteDatabase(18436):在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)   12-03 10:17:11.698:E / SQLiteDatabase(18436):at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)   12-03 10:17:11.698:E / SQLiteDatabase(18436):在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)   12-03 10:17:11.698:E / SQLiteDatabase(18436):at com.android.providers.contacts.ContactsProvider2.insertGroup(ContactsProvider2.java:5170)   12-03 10:17:11.698:E / SQLiteDatabase(18436):at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:3598)   12-03 10:17:11.698:E / SQLiteDatabase(18436):at com.android.providers.contacts.AbstractContactsProvider.insert(AbstractContactsProvider.java:116)   12-03 10:17:11.698:E / SQLiteDatabase(18436):at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2681)   12-03 10:17:11.698:E / SQLiteDatabase(18436):在android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)   12-03 10:17:11.698:E / SQLiteDatabase(18436):at com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:217) ....

2 个答案:

答案 0 :(得分:1)

我发现了我的问题而且非常愚蠢。

我写了ContactsContract.**Groups**.CONTENT_URI而不是ContactsContract.**Data**.CONTENT_URI

这就是我在sql中获得异常的原因。因为Groups表没有名为mimetype的列

答案 1 :(得分:0)

试试这段代码。 Groups._ID将由Android生成

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

    ops.add(ContentProviderOperation.newInsert(Groups.CONTENT_URI)
             .withValue(Groups.TITLE, "your_group_Title")
             .withValue(Groups.GROUP_VISIBLE, true)
             .withValue(RawContacts.ACCOUNT_TYPE, "com.google")
             .withValue(RawContacts.ACCOUNT_NAME, "********@gmail.com")
             .build());


    try {
        ContentProviderResult[] contactUri = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }