如何生成从一个对象,其中包含联系人细节和对象.vcf文件是不是在电话簿 [英] How to generate a .vcf file from an object which contains contact detail and object is not in the phone book

查看:161
本文介绍了如何生成从一个对象,其中包含联系人细节和对象.vcf文件是不是在电话簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要生成的对象,其中包含的联系信息,如姓名,图像,电话号码,传真号码,电子邮件ID一个.vcf文件,地址等,此对象不会在电话的地址簿中加入,但它是存储在我的应用程序。

I want to generate a .vcf file for an object which contains contact information like name, image, phone number, fax number, email id, address etc. This object is not added in the address book of the phone but it is stored in my application.

在我的.vcf文件生成我可以把这个名片像这样

Once my .vcf file is generated I can send this vcard like this

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("**generated.vcf**"), "text/x-vcard");
startActivity(i);

但我没有得到如何得到这个生成的.vcf文件?

But I am not getting how to get this generated .vcf file?

推荐答案

这其实很容易产生一个.vcf文件。看一看 VCF格式 - 这是一个简单的文本文件。所有你需要做的就是创建文本文件,然后使用VCF领域的信息写​​入到它。你最终的东西是这样的:

It's actually quite easy to generate a .vcf file. Have a look at VCF format - it's a simple text file. All you need to do is create text file and write info into it using the VCF fields. You'll end up with something like this:

Person p = getPerson();

File vcfFile = new File(this.getExternalFilesDir(null), "generated.vcf");
FileWriter fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:3.0\r\n");
fw.write("N:" + p.getSurname() + ";" + p.getFirstName() + "\r\n");
fw.write("FN:" + p.getFirstName() + " " + p.getSurname() + "\r\n");
fw.write("ORG:" + p.getCompanyName() + "\r\n");
fw.write("TITLE:" + p.getTitle() + "\r\n");
fw.write("TEL;TYPE=WORK,VOICE:" + p.getWorkPhone() + "\r\n");
fw.write("TEL;TYPE=HOME,VOICE:" + p.getHomePhone() + "\r\n");
fw.write("ADR;TYPE=WORK:;;" + p.getStreet() + ";" + p.getCity() + ";" + p.getState() + ";" + p.getPostcode() + ";" + p.getCountry() + "\r\n");
fw.write("EMAIL;TYPE=PREF,INTERNET:" + p.getEmailAddress() + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(vcfFile), "text/x-vcard");
startActivity(i);

(请注意,此code是放置一个活动里面,如果它不是一个活动,然后替换 getExternalFilesDir 与上下文)的一个实例

您可以有更多的更少的字段中,根据需要。如果你有; \ 字符领域价值观,他们需要与转义\ ;把一个换行符到一个值,写 \\ñ到文件(即本身必须包含文件,第二个斜线是转义新行斜线)。

You can have more of fewer fields, as needed. If you have ,, ; or \ characters in field values, they need to be escaped with \; to put a newline character into a value, write \\n into the file (i.e. the file itself must contain \n, the second slash is for escaping the slash in the newline).

这code是相当粗糙,但它应该让你开始。同样,看一下VCF格式,并从那里走了。

This code is quite crude, but it should get you started. Again, have a look at the format of VCF and go from there.

更新:感谢@迈克尔您指出的错误在我原来的答案

Update: Thanks to @Michael for pointing out mistakes in my original answers.

这篇关于如何生成从一个对象,其中包含联系人细节和对象.vcf文件是不是在电话簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆