Android 保存联系人,包括部门\职位\传真\地址\照片

private void toSaveContactInfo() {  
        ContentValues values = new ContentValues();  
        // 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId  
        Uri rawContactUri = context.getContentResolver().insert(  
                RawContacts.CONTENT_URI, values);  
        long rawContactId = ContentUris.parseId(rawContactUri);  
  
        // 往data表入姓名数据  
        values.clear();  
        values.put(Data.RAW_CONTACT_ID, rawContactId);  
        values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);  
        values.put(StructuredName.GIVEN_NAME, mEmployee.getEmployeeName());  
        context.getContentResolver().insert(  
                android.provider.ContactsContract.Data.CONTENT_URI, values);  
        // 添加用户名 职位 部门  
        values.clear();  
        values.put(  
                android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                rawContactId);  
        values.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);  
        values.put(Organization.LABEL, mEmployee.getUserName());  
        values.put(Organization.TITLE, mEmployee.getJobName());  
        values.put(Organization.COMPANY, mEmployee.getOrgName());  
        values.put(Organization.TYPE, Organization.TYPE_WORK);  
        context.getContentResolver().insert(  
                android.provider.ContactsContract.Data.CONTENT_URI, values);  
        // 添加邮箱  
        values.clear();  
        values.put(  
                android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                rawContactId);  
        values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);  
        values.put(Email.DATA, mEmployee.getOfficeEmail());  
        values.put(Email.TYPE, Email.TYPE_WORK);  
        context.getContentResolver().insert(  
                android.provider.ContactsContract.Data.CONTENT_URI, values);  
        // 添加手机  
        if (mEmployee.getMobile().contains("或")) {  
            final String[] tNumbers = mEmployee.getMobile().split("或");  
            for (String tNum : tNumbers) {  
                values.clear();  
                values.put(  
                        android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                        rawContactId);  
                values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
                values.put(Phone.NUMBER, tNum);  
                values.put(Phone.TYPE, Phone.TYPE_WORK_MOBILE);  
                context.getContentResolver().insert(  
                        android.provider.ContactsContract.Data.CONTENT_URI,  
                        values);  
            }  
        } else {  
            values.clear();  
            values.put(  
                    android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                    rawContactId);  
            values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
            values.put(Phone.NUMBER, mEmployee.getMobile());  
            values.put(Phone.TYPE, Phone.TYPE_WORK_MOBILE);  
            context.getContentResolver().insert(  
                    android.provider.ContactsContract.Data.CONTENT_URI, values);  
        }  
        // 添加固定电话  
        values.clear();  
        values.put(  
                android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                rawContactId);  
        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
        values.put(Phone.NUMBER, mEmployee.getOfficeTel());  
        values.put(Phone.TYPE, Phone.TYPE_WORK);  
        context.getContentResolver().insert(  
                android.provider.ContactsContract.Data.CONTENT_URI, values);  
        // 添加传真  
        values.clear();  
        values.put(  
                android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                rawContactId);  
        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
        values.put(Phone.NUMBER, mEmployee.getFax());  
        values.put(Phone.TYPE, Phone.TYPE_FAX_WORK);  
        context.getContentResolver().insert(  
                android.provider.ContactsContract.Data.CONTENT_URI, values);  
        // 添加地址 邮编  
        values.clear();  
        values.put(  
                android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                rawContactId);  
        values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);  
        values.put(StructuredPostal.FORMATTED_ADDRESS, mEmployee.getAddress());  
        values.put(StructuredPostal.POSTCODE, mEmployee.getPostCode());  
        values.put(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK);  
        context.getContentResolver().insert(  
                android.provider.ContactsContract.Data.CONTENT_URI, values);  
        // 添加头像  
        values.clear();  
        values.put(  
                android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,  
                rawContactId);  
        values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);  
        // if (mEmployee.getGender().equals("2"))// 为女,保存女性默认头像  
        // values.put(Photo.PHOTO,  
        // getDrawableToByte(R.drawable.oa_contact_head_f));  
        // else  
        // 男  
        values.put(Photo.PHOTO, getDrawableToByte(R.drawable.oa_contact_head_m));  
        context.getContentResolver().insert(  
                android.provider.ContactsContract.Data.CONTENT_URI, values);  
        // waitingDialog.dismiss();  
        isSave = true;  
        // Toast.makeText(context,  
        // "已保存" + mEmployee.getEmployeeName() + "的联系信息至本地通讯录",  
        // Toast.LENGTH_SHORT).show();  
    }


你可能感兴趣的:(Android 保存联系人,包括部门\职位\传真\地址\照片)