我想检索联系人的图片并在BitmapField
s中显示
所以我使用以下代码从联系人收集Bitmap对象:
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
?
答案 0 :(得分: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);
}