【问题标题】:Getting all contacts as vcard in Android在Android中将所有联系人作为电子名片
【发布时间】:2012-01-23 21:07:39
【问题描述】:

我正在尝试将我的所有联系人作为 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”(妈妈)并且这段代码无法识别它。像“António”这样的所有其他名字似乎都没有。

我被困了很长时间。

【问题讨论】:

    标签: android android-contacts vcf-vcard


    【解决方案1】:

    如果您想使用“内置”的 android 类来生成您的 vcard,那么它非常简单。

    public ArrayList<String> getContactsAsVcards()
    {
        ArrayList<String> vcards = new ArrayList<String>();
        VCardComposer vCardComposer = new VCardComposer(context);
    
        vCardComposer.init();
    
        do {
            String vCard = vCardComposer.createOneEntry();
    
            vcards.add(vCard);
        } while (!vCardComposer.isAfterLast());
    
        return vcards;
    
    }
    

    您只需从 android 源代码下载 android 类并将它们添加到您的项目中,并使用正确的包名称 com.android.vcardcom.android.vcard.exception。 您可以从here 下载课程。这适用于 Android 4.4 r1 版本。

    对你来说可能已经晚了,但如果有人正在寻找一种更强大和更好的方法来从他们的联系人列表中获取电子名片并让 android 在内部处理所有工作,这就是 IMO。

    【讨论】:

      【解决方案2】:

      获得 VCard 的最佳方法是编写自己的函数来创建 VCard。当联系人不是英文时,您使用的方式会出现问题。 手动获取联系人的所有字段并将其写入您的 VCard 文件。

      查看https://code.google.com/p/android-vcard/ 以读取和写入 VCard

      【讨论】:

      • 感谢您的回复。节日快乐 =)
      猜你喜欢
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多