[英]How do I export contacts to vcard?
有什么通用的方法可以將聯系人信息導出到Android中的vcard 3.0文件中? 我目前正在做一個學校項目,我想將android和ios上的所有本地聯系人上載到Web服務,以使從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,名稱和數字; 我還想知道聯系人是否有照片,以及它是私人電話還是公司電話。
謝謝您的幫助!
我自己做了,但是,這通常只給您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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.