显示我的联系方式照片来自PIM的黑莓联系人列表?

时间:2012-06-02 05:05:48

标签: blackberry contact

请有人告诉我如何使用此代码,我想在屏幕上显示联系人照片。从getPhoto()方法获取null。我搜索了很多但除了Contact Interface的代码之外什么都没有。但它不适合我...... !!!

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

1 个答案:

答案 0 :(得分: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);