在 Android 中获取所有联系人作为 vcard
我正在尝试以 vCard 形式获取我的所有联系人。
这是我的代码:
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());
}
}
这给了我几乎所有的联系人,但它不会返回一些包含特殊字符的联系人,在葡萄牙语中我们说“mãe”(妈妈),而此代码无法识别它。所有其他名字,如“安东尼奥”,似乎都没有。
我被困在这个问题里很久了。
I'm trying to get all of my contacts as vCard.
So this is my 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());
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用“内置”android 类来生成您的电子名片,那么这非常简单。
您只需从 Android 源代码下载 Android 类,并将它们添加到您的项目中,并使用正确的包名称:
com.android.vcard
和com.android.vcard.exception
代码>.您可以从 此处。适用于 Android 4.4 r1 版本。
可能对你来说已经太晚了,但万一有人正在寻找一种更强大、更好的方法来从他们的联系人列表中获取电子名片,并让 android 内部处理所有工作,这就是我的看法。
If you want to use the "built in" android classes to generate your vcards, then it's quite simple.
You just have to download the android classes from the android source code and add them to your project under the correct package name,
com.android.vcard
andcom.android.vcard.exception
.You can download the classes from here. That is for Android 4.4 r1 version.
Probably to late for you but in case someone is looking for a more robust and nice way to get the vcards from their contact list and letting android internally take care of all the work, this is it IMO.
获得 VCard 的最佳方法是编写自己的函数来创建 VCard。当联系人不是英文时,您使用的方法会出现问题。
手动获取联系人的所有字段并将其写入您的 VCard 文件。
看看https://code.google.com/p/android-vcard/ 用于读取和写入 VCard
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.
Take a look at https://code.google.com/p/android-vcard/ for reading from and writing to a VCard