How to retrieve PIM contact's photo as Bitmap using RIM5.0?
-
15-06-2021 - |
Question
I want to retrieve contacts' images and display them in BitmapField
s.
So I'm collecting Bitmap objects from contacts, using this code:
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++;
}
Unfortunately the code throws a java.lang.IllegalArumentException
at this method:
EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
How shoud I convert byte[]
image to BitmapField
?
Solution
I found the solution for those who are interested, images retrieved from PIM are Base64 encoded, it should be decoded first. Here's the correct code:
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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow