请任何人告诉我如何使用此代码,我想在屏幕上显示联系人照片。从 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();
}