如何将联系人导出到 vcard?
How do I export contacts to vcard?
在 android 中是否有任何通用方法可以将联系人信息导出到 vcard 3.0 文件?我目前正在做一个学校项目,我想将所有本地联系人表单 android 和 ios 上传到网络服务,以便更轻松地从 android 到 android、android 到 ios,反之亦然。
然而,不幸的是,在Android中,一次获取所有联系信息是相当困难的,因此,我想有一个通用的方法(例如,一些联系人没有个人资料照片,而其他人做)。
我遇到过一些变体来解决这个问题,但是,我不喜欢它们。
这是我当前检索联系人的代码:
public void getAndroidContacts(){
// get contacts
Cursor cursor_android_contacts = null;
ContentResolver contentResolver = getContentResolver();
try {
cursor_android_contacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
}catch (Exception e) {
Log.e("Error on contact", e.getMessage());
}
// check hasContacts
if (cursor_android_contacts.getCount()>0){
while (cursor_android_contacts.moveToNext()){
ContactSerializer.Android_Contact android_contact = new ContactSerializer.Android_Contact();
// Get Name & Contact_ID
String display_name=cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contact_ID = cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
android_contact.android_contact_ID = Integer.parseInt(contact_ID);
android_contact.android_contact_name = display_name;
// get Number
int hasPhoneNumber = Integer.parseInt(cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0){
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contact_ID},
null);
while (phoneCursor.moveToNext()){
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// set number
android_contact.android_contact_number = phoneNumber;
}
phoneCursor.close();
}
ContactSerializer.localContacts.add(android_contact);
}
localContactCount = cursor_android_contacts.getCount();
}// hasContacts
}
使用这段代码,我只得到了id、name和number;我还想知道联系人是否有照片,是私人号码还是公司号码。
感谢您的帮助!
我自己设法做到了,但是,这通常只能为您提供 vCard V2.1:
private void getVcardString() {
// TODO: 07.11.2017 Fix vCard Version
vCardArray = new ArrayList<>();
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String vCard = new String(buf);
vCardArray.add(vCard);
Log.println(Log.ASSERT, "Vcard", vCard);
} catch (Exception e1) {
e1.printStackTrace();
}
// you could save the file here using a FileOutputStream
cursor.moveToNext();
}
} else {
Log.d("vCardERROR", "No Contacts in Your Phone");
}
}
在 android 中是否有任何通用方法可以将联系人信息导出到 vcard 3.0 文件?我目前正在做一个学校项目,我想将所有本地联系人表单 android 和 ios 上传到网络服务,以便更轻松地从 android 到 android、android 到 ios,反之亦然。
然而,不幸的是,在Android中,一次获取所有联系信息是相当困难的,因此,我想有一个通用的方法(例如,一些联系人没有个人资料照片,而其他人做)。
我遇到过一些变体来解决这个问题,但是,我不喜欢它们。
这是我当前检索联系人的代码:
public void getAndroidContacts(){
// get contacts
Cursor cursor_android_contacts = null;
ContentResolver contentResolver = getContentResolver();
try {
cursor_android_contacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
}catch (Exception e) {
Log.e("Error on contact", e.getMessage());
}
// check hasContacts
if (cursor_android_contacts.getCount()>0){
while (cursor_android_contacts.moveToNext()){
ContactSerializer.Android_Contact android_contact = new ContactSerializer.Android_Contact();
// Get Name & Contact_ID
String display_name=cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String contact_ID = cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
android_contact.android_contact_ID = Integer.parseInt(contact_ID);
android_contact.android_contact_name = display_name;
// get Number
int hasPhoneNumber = Integer.parseInt(cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0){
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contact_ID},
null);
while (phoneCursor.moveToNext()){
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// set number
android_contact.android_contact_number = phoneNumber;
}
phoneCursor.close();
}
ContactSerializer.localContacts.add(android_contact);
}
localContactCount = cursor_android_contacts.getCount();
}// hasContacts
}
使用这段代码,我只得到了id、name和number;我还想知道联系人是否有照片,是私人号码还是公司号码。
感谢您的帮助!
我自己设法做到了,但是,这通常只能为您提供 vCard V2.1:
private void getVcardString() {
// TODO: 07.11.2017 Fix vCard Version
vCardArray = new ArrayList<>();
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String vCard = new String(buf);
vCardArray.add(vCard);
Log.println(Log.ASSERT, "Vcard", vCard);
} catch (Exception e1) {
e1.printStackTrace();
}
// you could save the file here using a FileOutputStream
cursor.moveToNext();
}
} else {
Log.d("vCardERROR", "No Contacts in Your Phone");
}
}