Android如何导出VCard

记录一下最近的东西,但是有一点,Android导出来的VCard只能是V2.1的不能与V3.0的互相导入。

这里记录一段代码。

获取联系人,提取lookupKey

	public static List<PhoneContact> getPhoneContact(Context context) {
        List<PhoneContact> list = new ArrayList<PhoneContact>();
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,
                null, null,
                null, null);
        if (cursor == null || !cursor.moveToFirst()) {
        	if (cursor != null) {
                cursor.close();
            }
            return list;
        }
        while (!cursor.isAfterLast()) {
            PhoneContact contact = new PhoneContact();
            contact._id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
            contact.lookupKey = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.LOOKUP_KEY));
            contact.displayName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if (phoneCount > 0) {
                list.add(contact);
            }
            cursor.moveToNext();
        }
        if (cursor != null) {
            cursor.close();
        }
        return list;
    }

从lookupKey获取VCard

    public static String getVCard(Context context, String lookupKey) {
        String vCard = "";
        Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_VCARD_URI,
                lookupKey);
        AssetFileDescriptor fd;
        try {
            fd = context.getContentResolver()
                    .openAssetFileDescriptor(uri, "r");
            FileInputStream fis = fd.createInputStream();
            byte[] b = new byte[(int) fd.getDeclaredLength()];
            fis.read(b);
            vCard = new String(b);
            Log.i("GuoQi", vCard);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return vCard;
    }
最后是导入通讯录

public static void importContactByVCard(Context context,String filePath){
        Uri uri = Uri.parse("file://"+filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "text/x-vcard");
        //android系统版本不同,所以系统中的activity也会不同。
        try{
            intent.setComponent(
                    new ComponentName("com.android.contacts","com.android.contacts.vcard.ImportVCardActivity"));
            context.startActivity(intent);
        }catch(Exception e){
            try{
                intent.setComponent(
                        new ComponentName("com.android.contacts","com.android.contacts.ImportVCardActivity"));
                context.startActivity(intent);
            }catch(Exception e1){
                try{
                    intent.setComponent(
                            new ComponentName("com.android.contacts","com.android.contacts.common.vcard.ImportVCardActivity"));
                    context.startActivity(intent);
                }catch(Exception e2){
                    intent.setComponent(null);
                    context.startActivity(intent);
                }

            }
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值