以编程方式从android删除联系人图片

时间:2012-12-22 00:14:33

标签: android-contacts

我正在尝试通过代码删除联系人图片。我通过将Photo.PHOTO设置为null来尝试此操作。但是,该语句会抛出异常。这是代码:

ContentValues values = new ContentValues();
int             result = 0;

values.put(Data.RAW_CONTACT_ID, contactId);
values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
values.put(Photo.PHOTO, (byte[])null);

result = getContext().getContentResolver().update(Data.CONTENT_URI, values, Data.RAW_CONTACT_ID + "=?", new String[]{String.valueOf(contactId)});

这是例外

  

android.database.sqlite.SQLiteException:未知错误(代码0):无法将BLOB转换为字符串       在android.database.CursorWindow.nativeGetString(本机方法)       在android.database.CursorWindow.getString(CursorWindow.java:434)       在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)       在com.android.providers.contacts.DataRowHandler.getAugmentedValues(DataRowHandler.java:332)       在com.android.providers.contacts.DataRowHandlerForStructuredName.update(DataRowHandlerForStructuredName.java:70)       在com.android.providers.contacts.ContactsProvider2.updateData(ContactsProvider2.java:4507)       在com.android.providers.contacts.ContactsProvider2.updateData(ContactsProvider2.java:4490)       在com.android.providers.contacts.ContactsProvider2.updateInTransaction(ContactsProvider2.java:3875)       在com.android.providers.contacts.AbstractContactsProvider.update(AbstractContactsProvider.java:143)       在com.android.providers.contacts.ContactsProvider2.update(ContactsProvider2.java:2061)       在android.content.ContentProvider $ Transport.update(ContentProvider.java:235)       在android.content.ContentResolver.update(ContentResolver.java:990)

1 个答案:

答案 0 :(得分:0)

以下是删除照片的方法(基于this post):

ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);

builder.withSelection(ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "=?",
        new String[] { ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE });
builder.withValue(Integer.toString(id),ContactsContract.CommonDataKinds.Photo.PHOTO, (byte[0])null);

final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(builder.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);