0

我有一个位图和一个联系人 ID。我想要一个接受这些参数并将位图设置为该 ID 的联系人图片的函数。你能帮我吗?

4

1 回答 1

1

尝试

将您的位图转换为 byteArray

Bitmap bit; // <-- put your bitmap here
ByteArrayOutputStream streamy = new ByteArrayOutputStream(); 
bit.compress(CompressFormat.PNG, 0, streamy); 
byte[] photo = streamy.toByteArray();

接着

ContentValues values = new ContentValues(); 
int photoRow = -1; 
String where = ContactsContract.Data.RAW_CONTACT_ID + " == " + 
    ContentUris.parseId(yourContectID) + " AND " + Data.MIMETYPE + "=='" + 
    ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'"; 
Cursor cursor = managedQuery(
        ContactsContract.Data.CONTENT_URI, 
        null, 
        where, 
        null, 
        null); 
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID); 
if(cursor.moveToFirst()){ 
    photoRow = cursor.getInt(idIdx); 
} 
cursor.close(); 
values.put(ContactsContract.Data.RAW_CONTACT_ID, 
        ContentUris.parseId(yourContectID)); 
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1); 
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, photo); 
values.put(ContactsContract.Data.MIMETYPE, 
        ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); 
if(photoRow >= 0){ 
    this.getContentResolver().update(
            ContactsContract.Data.CONTENT_URI, 
            values, 
            ContactsContract.Data._ID + " = " + photoRow, null); 
    } else { 
        this.getContentResolver().insert(
                ContactsContract.Data.CONTENT_URI, 
                values); 
    } 
} 

不要忘记添加权限WRITE_CONTACTSREAD_CONTACTS在您的manifest file

于 2012-07-15T12:36:36.603 回答