问题描述
我试图让我的所有联系人的电子名片为
I'm trying to get all of my contacts as vCard.
这就是我的code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
//cur.moveToFirst();
while (cur.moveToNext()) {
try{
String lookupKey =
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri =
Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI,
lookupKey);
AssetFileDescriptor fd =
this.getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int)fd.getDeclaredLength()];
if (0 < fis.read(buf))
{
String vCard = new String(buf);
System.out.println("The vCard value is " + vCard);
}
fis.close();
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
}
cur.close();
System.out.println(cur.getCount());
}
}
这给了我几乎所有的联系,但它不会返回一个包含一些特殊的字符,葡萄牙语说美(妈妈),这code不承认它的一些联系。所有其他名称,如安东尼奥不会出现。
This gives me almost all of the contacts, but it doesn't return some contacts that contains some special characters, in Portuguese we say "mãe" (mom) and this code doesn't recognize it. All other names like "António" doesn't appear to.
我被困在这个很长一段时间。
I'm stuck in this for a long time.
推荐答案
要得到一个电子名片的最好方法是写自己的函数来创建它使用的是有问题时,联系人不在英语VCard.The方式。
手动获取联系人的所有字段,并写信给你的电子名片的文件。
The best way to get a VCard is to write your own function to create a VCard.The way which you are using has problems when Contacts are not in English.Fetch all the fields of a contact manually and write it to your VCard file.
看看从读取和写入到vCard
Take a look at https://code.google.com/p/android-vcard/ for reading from and writing to a VCard
这篇关于获得所有通讯录在Android的电子名片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!