How to retrieve PIM contact's photo as Bitmap

2019-08-31 16:30发布

问题:

I want to retrieve contacts' images and display them in BitmapFields.
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 ?

回答1:

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);
}