1

请任何人告诉我如何使用此代码,我想在屏幕上显示联系人照片。从 getPhoto() 方法获取 null。我进行了很多搜索,但除了来自联系人界面的这段代码外,一无所获。但它不适合我.....!!!

    public class PhotoExample {

    private Contact _contact;

    public PhotoExample(Contact contact) throws PIMException {
        ContactList contactList = (ContactList) 
            PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
        _contact = contactList.createContact();
        /*_contact = contact;*/
    }

    public void setPhoto() throws IOException {
        byte[] photo = getSamplePhoto();
        byte[] photoEncoded = Base64OutputStream.encode(photo, 0, photo.length, false, false);
        if (_contact.countValues(Contact.PHOTO) > 0) {
            _contact.setBinary(Contact.PHOTO, 0, PIMItem.ATTR_NONE, photoEncoded, 0, photo.length);
        } else {
            _contact.addBinary(Contact.PHOTO, PIMItem.ATTR_NONE, photoEncoded, 0, photo.length);
        }
    }

    public byte[] getPhoto() throws IOException {
        if (_contact.countValues(Contact.PHOTO) > 0) {
            byte[] photoEncoded = _contact.getBinary(Contact.PHOTO, 0);
            return Base64InputStream.decode(photoEncoded, 0, photoEncoded.length);
        } else {
            return null;
        }
    }

    private static byte[] getSamplePhoto() {
        return null;
        // return the raw bytes of the photo to use
    }

    /*public static void main(String[] args) throws Throwable {
        PhotoExample example = new PhotoExample();
        example.setPhoto();
        example.getPhoto();
    }*/

}

我使用上面的代码是这样的: -

try {
            _photo = new PhotoExample(_contact);
             b = _photo.getPhoto();
        } catch (PIMException e) {
            System.out.println(e+"===>");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(e+"===>");
            e.printStackTrace();
        }
4

1 回答 1

2

很长一段时间后,经过几次搜索,我再次尝试了这个任务,现在我成功了,想分享一下,因为这段代码可能对很多人有用。

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 = null;
                try {
                    imageBytes = Base64InputStream.decode(imageBytesBase64, 0, imageBytesBase64.length);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                EncodedImage encodedImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
                Bitmap bitmap = encodedImage.getBitmap();
                bitmaps.addElement(bitmap);
                BitmapField fd= new BitmapField(bitmap, Field.FIELD_HCENTER);
                add(fd); 
于 2012-07-13T08:35:33.397 回答