0

我想检索联系人的图像并将它们显示在BitmapFields.
所以我正在使用以下代码从联系人中收集位图对象:

Vector bitmaps = new Vector();
BlackBerryContactList contactList = (BlackBerryContactList)BlackBerryPIM.getInstance().openPIMList(BlackBerryPIM.CONTACT_LIST, BlackBerryPIM.READ_WRITE);
Enumeration contactListItems = contactList.items();
int counter = 0;
while (contactListItems.hasMoreElements()) {
    BlackBerryContact contact = (BlackBerryContact)contactListItems.nextElement();
    byte[] imageBytes = contact.getBinary(BlackBerryContact.PHOTO, counter);
    EncodedImage encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
    Bitmap bitmap = encodedImage.getBitmap();
    bitmaps.addElement(bitmap);
    counter++;
}

java.lang.IllegalArumentException不幸的是,代码在这个方法中抛出了一个:

EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);

我应该如何将byte[]图像转换为BitmapField

4

1 回答 1

3

我为那些感兴趣的人找到了解决方案,从 PIM 检索到的图像是 Base64 编码的,应该先解码。这是正确的代码:

Vector bitmaps = new Vector();
BlackBerryContactList contactList = (BlackBerryContactList)BlackBerryPIM.getInstance().openPIMList(BlackBerryPIM.CONTACT_LIST, BlackBerryPIM.READ_WRITE);
Enumeration contactListItems = contactList.items();
while (contactListItems.hasMoreElements()) {
    BlackBerryContact contact = (BlackBerryContact)contactListItems.nextElement();
    byte[] imageBytesBase64 = contact.getBinary(BlackBerryContact.PHOTO, 0);
    byte[] imageBytes = Base64InputStream.decode(imageBytesBase64, 0, imageBytesBase64.length);
    EncodedImage encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
    Bitmap bitmap = encodedImage.getBitmap();
    bitmaps.addElement(bitmap);
}
于 2012-06-18T15:08:55.437 回答