How to retrieve PIM contact's photo as Bitmap using RIM5.0?

时光怂恿深爱的人放手 提交于 2019-12-25 05:08:55

问题


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


来源:https://stackoverflow.com/questions/11084775/how-to-retrieve-pim-contacts-photo-as-bitmap-using-rim5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!