当联系人没有时,使用ContactsContract更新电子邮件

时间:2017-12-06 15:26:35

标签: java android contactscontract

我想更新联系人的邮件地址,它似乎有效但我联系的情况是联系人没有邮件地址它没有工作,我有这个错误:

#################################################################
W/System.err: Error Code : 0 (SQLITE_OK)
W/System.err: Caused By : unknown error (code 0): Unable to convert BLOB to string
#################################################################

这是我的函数updateMail()

public static boolean updateMail(Context context, String id, String email){
        ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();

        builder.withSelection(ContactsContract.Data.CONTACT_ID + "=? " + "AND "
                        + ContactsContract.CommonDataKinds.Organization.TYPE + "=?"
                , new String[]{id,
                        String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_HOME)});
        builder.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
        ops.add(builder.build());

        return (applyBatch(context, ops));
    }

当联系人没有邮件地址时,我可能需要插入而不是更新,您怎么看?

编辑:

我试试这个,但它不起作用......也许我没有使用插页做出正确的选择:

public static boolean updateMail(Context context, String id, String email){
        ContentProviderOperation.Builder builder;
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();

        if (getMailById(context, id).isEmpty()){

            builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);

            builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, Integer.valueOf(id));
            builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
            builder.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
            builder.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME);
            ops.add(builder.build());

        } else {

            builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);

            builder.withSelection(ContactsContract.Data.CONTACT_ID + "=? " + "AND "
                            + ContactsContract.CommonDataKinds.Organization.TYPE + "=?"
                    , new String[]{id,
                            String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_HOME)});
            builder.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
            ops.add(builder.build());

        }

        return (applyBatch(context, ops));
    }

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我必须使用ContactsContract.Data.RAW_CONTACT_ID

public static boolean updateMail(Context context, String id, String email){
        ContentProviderOperation.Builder builder;
        ArrayList<ContentProviderOperation> ops = new ArrayList<>();

        if (getMailById(context, id).isEmpty()){
            builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
            builder.withValue(ContactsContract.Data.RAW_CONTACT_ID, getRawId(context, Long.parseLong(id)));
            builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
            builder.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
            ops.add(builder.build());
        } else {
            builder = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);

            builder.withSelection(ContactsContract.Data.CONTACT_ID + "=? " + "AND "
                            + ContactsContract.CommonDataKinds.Organization.TYPE + "=?"
                    , new String[]{id,
                            String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_HOME)});
            builder.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
            ops.add(builder.build());

        }

        return (applyBatch(context, ops));
    }

方法getRawId()

private static long getRawId(Context context, long contactId) {
        String selection = ContactsContract.RawContacts.CONTACT_ID + "='" + contactId + "'";
        try (Cursor cur = context.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts._ID}, selection, null, null)) {
            assert cur != null;
            if (cur.moveToNext()) { return cur.getLong(0);}
        }
        return 0;
    }