当我添加联系人但不将他添加到群组时,它可以工作,但是当
我尝试添加到特定组(我有ID),但失败了。
我读了很多关于如何将联系人添加到组的答案。但它一直失败。
这是我的代码:
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());
我在日志中找到了此跟踪:
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 = 10dirty = 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,脏,raw_contact_id)值(?,?,?,?,?)
12-03 10:17:11.698:E / SQLiteDatabase(18436):位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本地方法)
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):位于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):位于com.android.providers.contacts.ContactsProvider2.insertGroup(ContactsProvider2.java:5170)
12-03 10:17:11.698:E / SQLiteDatabase(18436):在com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:3598)
12-03 10:17:11.698:E / SQLiteDatabase(18436):在com.android.providers.contacts.AbstractContactsProvider.insert(AbstractContactsProvider.java:116)
12-03 10:17:11.698:E / SQLiteDatabase(18436):在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):在com.android.providers.contacts.AbstractContactsProvider.applyBatch(AbstractContactsProvider.java:217)....
最佳答案
我发现了问题,这很愚蠢。
我写这个ContactsContract.**Groups**.CONTENT_URI
代替ContactsContract.**Data**.CONTENT_URI
这就是为什么我在sql中得到异常。因为到“组”表中没有名为mimetype的列