ezvcard.property
Class Photo

java.lang.Object
  extended by ezvcard.property.VCardProperty
      extended by ezvcard.property.BinaryProperty<ImageType>
          extended by ezvcard.property.ImageProperty
              extended by ezvcard.property.Photo
All Implemented Interfaces:
HasAltId, Comparable<VCardProperty>

public class Photo
extends ImageProperty

A photo attached to the vCard (such as a portrait of the person).

Adding a photo

 VCard vcard = new VCard();
 
 //URL
 Photo photo = new Photo("http://www.mywebsite.com/mugshot.jpg", ImageType.JPEG);
 vcard.addPhoto(photo);
 
 //binary data
 byte data[] = ...
 photo = new Photo(data, ImageType.JPEG);
 vcard.addPhoto(photo);
 
 //if "ImageType" does not have the pre-defined constant that you need, then create a new instance
 //arg 1: the value of the 2.1/3.0 TYPE parameter
 //arg 2: the value to use for the 4.0 MEDIATYPE parameter and for 4.0 data URIs
 //arg 3: the file extension of the data type (optional)
 ImageKeyTypeParameter param = new ImageType("bmp", "image/x-ms-bmp", "bmp");
 photo = new Photo("http://www.mywebsite.com/mugshot.bmp", param);
 vcard.addPhoto(photo);
 

Getting the photos

 VCard vcard = ...
 
 int fileCount = 0;
 for (Photo photo : vcard.getPhotos()){
   //the photo will have either a URL or a binary data
   if (photo.getData() == null){
     System.out.println("Photo URL: " + photo.getUrl());
   } else {
     ImageType type = photo.getContentType();
     
     if (type == null) {
       //the vCard may not have any content type data associated with the photo
       System.out.println("Saving a photo file...");
     } else {
       System.out.println("Saving a \"" + type.getMediaType() + "\" file...");
     }
     
     String folder;
     if (type == ImageType.JPEG){ //it is safe to use "==" instead of "equals()"
       folder = "photos";
     } else {
       folder = "images";
     }
     
     byte data[] = photo.getData();
     String filename = "photo" + fileCount;
     if (type != null && type.getExtension() != null){
        filename += "." + type.getExtension();
     }
     OutputStream out = new FileOutputStream(new File(folder, filename));
     out.write(data);
     out.close();
     fileCount++;
   }
 }
 

Property name: PHOTO

Supported versions: 2.1, 3.0, 4.0

Author:
Michael Angstadt

Field Summary
 
Fields inherited from class ezvcard.property.BinaryProperty
contentType, data, url
 
Fields inherited from class ezvcard.property.VCardProperty
group, parameters
 
Constructor Summary
Photo(byte[] data, ImageType type)
          Creates a photo property.
Photo(File file, ImageType type)
          Creates a photo property.
Photo(InputStream in, ImageType type)
          Creates a photo property.
Photo(String url, ImageType type)
          Creates a photo property.
 
Method Summary
 
Methods inherited from class ezvcard.property.BinaryProperty
_validate, addPid, getAltId, getContentType, getData, getPids, getPref, getType, getUrl, removePids, setAltId, setContentType, setData, setPref, setType, setUrl
 
Methods inherited from class ezvcard.property.VCardProperty
_supportedVersions, addParameter, compareTo, getGroup, getParameter, getParameters, getParameters, getSupportedVersions, removeParameter, setGroup, setParameter, setParameters, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Photo

public Photo(String url,
             ImageType type)
Creates a photo property.

Parameters:
url - the URL to the photo
type - the content type (e.g. JPEG)

Photo

public Photo(byte[] data,
             ImageType type)
Creates a photo property.

Parameters:
data - the binary data of the photo
type - the content type (e.g. JPEG)

Photo

public Photo(InputStream in,
             ImageType type)
      throws IOException
Creates a photo property.

Parameters:
in - an input stream to the binary data (will be closed)
type - the content type (e.g. JPEG)
Throws:
IOException - if there's a problem reading from the input stream

Photo

public Photo(File file,
             ImageType type)
      throws IOException
Creates a photo property.

Parameters:
file - the image file
type - the content type (e.g. JPEG)
Throws:
IOException - if there's a problem reading from the file


Copyright © 2012-2014 Michael Angstadt. All Rights Reserved.