1

我正在尝试将联系人添加到具有特定联系人 ID 的联系人数据库(我正在使用 vcf 文件来获取联系人的详细信息),联系人 ID 保存在文件名(Dani_13)中,我需要将此联系人添加到其联系人中的原始条目。我一直在使用下面的代码,但它不起作用:

String [] s = (vcard_file.getName()).split(Pattern.quote("_")); 
         String [] s2 = s[(s.length)-1].split(Pattern.quote("."));
        // int rawContactInsertIndex=ops.size(); 
         int id = Integer.getInteger(s2[0]);
         ops.add(ContentProviderOperation
                 .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                 .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                 .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)              
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
                      .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, null)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
                      //.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
                      .build());
4

1 回答 1

0

试试这个:

 ContentResolver cr =  getContext().getContentResolver();
             Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                     null, null, null, null);

             if (cur.getCount() > 0) {
                 while (cur.moveToNext()) {
                       String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                       String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                       if (Integer.parseInt(cur.getString(
                             cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                          Cursor pCur = cr.query(
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                    new String[]{id}, null);
                          while (pCur.moveToNext()) {
                              String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                          }
                         pCur.close();
                     }
                 }
             }

为我工作。

于 2013-08-20T10:22:16.773 回答