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) 
  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值