这是Android联系人头像的bug吗

经过BASE64编码之后的图片字符串(点此下载,测试源码当中也有),在Java SE环境下可以解码出来生成一张图片,但是通过代码插入到Android联系人头像就无法显示出来(显示的一张白色图片,非Android Contacts原生的占位符头像),用SQLITE MAN查看插入图像后的数据库BLOB字段,显示“File content cannot be displayed”
目前还没有解决办法,测试了很多图片,有的图片有时候能显示,有时候不能显示,这是Android的bug吗?

插入图片的源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private void doInsert(String imageStr) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int rawContactInsertIndex = ops.size();
 
    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_TYPE, null).withValue(
                    RawContacts.ACCOUNT_NAME, null).withValue(
                    RawContacts.AGGREGATION_MODE,
                    RawContacts.AGGREGATION_MODE_DISABLED).build());
 
    ContentProviderOperation photoOp = ContentProviderOperation.newInsert(
            Data.CONTENT_URI).withValueBackReference(Data.RAW_CONTACT_ID,
            rawContactInsertIndex).withValues(
            getPhotoCV(B64.decode(imageStr))).build();
    ops.add(photoOp);
 
    try {
        getApplicationContext().getContentResolver().applyBatch(
                ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        e.printStackTrace();
    }
}
 
private ContentValues getPhotoCV(byte[] photo) {
    ContentValues cv = new ContentValues();
    cv.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
    cv.put(Photo.PHOTO, photo);
    return cv;
}

完整测试源码下载http://hb.good.gd/?FileId=1062335

Leave a Reply

Your email address will not be published. Required fields are marked *