Export a contact

You can export contact information from a contact list on a BlackBerry® device to an output stream. The export process converts a PIM item to a stream of bytes that another application can import. You can export PIM data to a supported serial format by invoking PIM.toSerialFormat(PIMItem, OutputStream, String, String), and passing in as arguments the PIMItem, the OutputStream to which the serialized PIMItem is written, the character encoding format to use when writing to the output stream, and the supported serial format to convert to, such as vCard®.

  1. Import the required classes and interfaces.
    import java.io.UnsupportedEncodingException;
    import java.util.Enumeration;
    import javax.microedition.pim.Contact;
    import javax.microedition.pim.ContactList;
    import javax.microedition.pim.PIM;
    import javax.microedition.pim.PIMException;
  2. Invoke PIM.supportedSerialFormats() and specify the list type (PIM.CONTACT_LIST) to retrieve a string array of the supported serial formats.
    ContactList contactList = (ContactList) 
       PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
    String[] dataFormats = PIM.getInstance().supportedSerialFormats(PIM.CONTACT_LIST);
  3. Invoke PIM.getInstance().toSerialFormat(item, stream, enc, dataFormat)to write an item to a supported serial format. Use the enc parameter to specify the character encoding format to use when writing to the output stream. Supported character encoding formats include UTF8, ISO-8859-1, and UTF-16BE. If the enc parameter is null, the method uses UTF-8.
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    Enumeration e = contactList.items();
    while (e.hasMoreElements())
          Contact c = (Contact)e.nextElement();
          PIM.getInstance().toSerialFormat(c, byteStream, "UTF8", dataFormats[0]);
       catch (UnsupportedEncodingException ex)
Next topic: Import a contact

Was this information helpful? Send us your comments.