* @param fileName
* the vcard file
* @return the created Contact
*/
public Contact readFromVCard(String fileName) {
Contact contact = new Contact();
BufferedReader bufferedReader = null;
String charSet = "Cp1252";
// parse the vCard
try {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(fileName), charSet);
bufferedReader = new BufferedReader(inputStreamReader);
String line;
String value;
while ((line = bufferedReader.readLine()) != null) {
value = getVCardValue(line, "N");
if (value != null) {
String[] result = value.split(";");
if (result.length > 0) {
contact.setLastName(result[0]);
}
if (result.length > 1) {
contact.setFirstName(result[1]);
}
if (result.length > 2) {
contact.setMiddleName(result[2]);
}
if (result.length > 3) {
contact.setTitle(result[3]);
}
continue;
}
value = getVCardValue(line, "TEL;WORK");
if (value != null) {
contact.setPhone(value);
continue;
}
value = getVCardValue(line, "TEL;CELL");
if (value != null) {
contact.setMobile(value);
continue;
}
value = getVCardValue(line, "ADR;WORK");
if (value != null) {
String[] result = value.split(";");
if (result.length > 2) {
contact.setStreet(result[2]);
}
if (result.length > 3) {
contact.setCity(result[3]);
}
if (result.length > 4) {
contact.setState(result[4]);
}
if (result.length > 5) {
contact.setZip(result[5]);
}
if (result.length > 6) {
contact.setCountry(result[6]);
}
continue;
}
value = getVCardValue(line, "EMAIL;PREF;INTERNET");
if (value != null) {
contact.setEmail(value);
continue;
}
value = getVCardValue(line, "URL;WORK");
if (value != null) {
contact.setWebPage(value);
continue;
}
value = getVCardValue(line, "ORG");
if (value != null) {
contact.setCompany(value);
continue;
}
value = getVCardValue(line, "TITLE");
if (value != null) {
contact.setJobTitle(value);
continue;
}
value = getVCardValue(line, "NOTE");
if (value != null) {
contact.setNote(value);
continue;
}
value = getVCardValue(line, "PHOTO;TYPE=JPEG;ENCODING=BASE64");
if (value != null) {
line = bufferedReader.readLine();
StringBuilder builder = new StringBuilder();
while (line != null && line.length() > 0 && line.charAt(0) == ' ') {
builder.append(line.trim());
line = bufferedReader.readLine();
}
String jpegString = builder.toString();
String base64 = "data:image/png;base64," + jpegString;
contact.setJpegString(base64);
continue;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block