问题
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
?
回答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