有没有通用的方法将联系信息导出到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;我还想知道联系人是否有照片以及是私人还是公司号码。
感谢您的帮助!
答案 0 :(得分:0)
我自己设法做到了,但是,这只能给你典型的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");
}
}