Android中使用VCard

  1. OutputStreamWriter writer = ...(此处略去 ) 
  2. VCardComposer composer = new VCardComposer(); 
  3. //create a contact  
  4. ContactStruct contact1 = new ContactStruct(); 
  5. contact1.name = "张三"
  6. contact1.company = "海辉集团"
  7. contact1.addPhone("+123456789", String.valueOf(Contacts.Phones.TYPE_MOBILE), null); 
  8. //create vCard representation  
  9. String vcardString = composer.createVCard(contact1, VCardComposer.VERSION_VCARD30_INT); 
  10. //write vCard to the output stream  
  11. writer.write(vcardString); 
  12. writer.write("/n"); //add empty lines between contacts 
  13. // repeat for other contacts  
  14. ... 
  15. writer.close(); 
  16. Reading vCard 
  17. VCardParser parser = new VCardParser(); 
  18. VDataBuilder builder = new VDataBuilder(); 
  19. //read whole file to string  
  20. //(FileUtils are part of Commons IO library, do it by hand if you don't use it) 
  21. String vcardString = FileUtils.readFileToString(file, "UTF-8"); 
  22. //parse the string  
  23. boolean parsed = parser.parse(vcardString, "UTF-8", builder); 
  24. if (!parsed) { 
  25. throw new VCardException("Could not parse vCard file: " + file); 
  26. //get all parsed contacts  
  27. List<VNode> pimContacts = builder.vNodeList; 
  28. //do something for all the contacts 
  29. for (VNode contact : pimContacts) { 
  30. ArrayList<PropertyNode> props = contact.propList; 
  31. //contact name - FN property  
  32. String name = null
  33. for (PropertyNode prop : props) { 
  34. if ("FN".equals(prop.propName)) { 
  35. name = prop.propValue; 
  36. //we have the name now  
  37. break
  38. //similarly for other properties (FN, ORG, TEL, etc) 
  39. [java] view plain copy print ?
  1. OutputStreamWriter writer = ...(此处略去 ) 
  2. VCardComposer composer = new VCardComposer(); 
  3. //create a contact 
  4. ContactStruct contact1 = new ContactStruct(); 
  5. contact1.name = "张三"
  6. contact1.company = "海辉集团"
  7. contact1.addPhone("+123456789", String.valueOf(Contacts.Phones.TYPE_MOBILE), null); 
  8. //create vCard representation 
  9. String vcardString = composer.createVCard(contact1, VCardComposer.VERSION_VCARD30_INT); 
  10. //write vCard to the output stream 
  11. writer.write(vcardString); 
  12. writer.write("/n"); //add empty lines between contacts 
  13. // repeat for other contacts 
  14. ... 
  15. writer.close(); 
  16. Reading vCard 
  17. VCardParser parser = new VCardParser(); 
  18. VDataBuilder builder = new VDataBuilder(); 
  19. //read whole file to string 
  20. //(FileUtils are part of Commons IO library, do it by hand if you don't use it) 
  21. String vcardString = FileUtils.readFileToString(file, "UTF-8"); 
  22. //parse the string 
  23. boolean parsed = parser.parse(vcardString, "UTF-8", builder); 
  24. if (!parsed) { 
  25. throw new VCardException("Could not parse vCard file: " + file); 
  26. //get all parsed contacts 
  27. List<VNode> pimContacts = builder.vNodeList; 
  28. //do something for all the contacts 
  29. for (VNode contact : pimContacts) { 
  30. ArrayList<PropertyNode> props = contact.propList; 
  31. //contact name - FN property 
  32. String name = null
  33. for (PropertyNode prop : props) { 
  34. if ("FN".equals(prop.propName)) { 
  35. name = prop.propValue; 
  36. //we have the name now 
  37. break
  38. //similarly for other properties (FN, ORG

你可能感兴趣的:(Android中使用VCard)