简体   繁体   English

Android-更新电话簿中的联系人

[英]Android - Update a contact in the phone book

I'm still working on my phone book app and I want to update a contact that is stored into the stock phone book. 我仍在使用电话簿应用程序,并且我想更新存储在库存电话簿中的联系人。 I'm able to insert a new contact but update has no effect at all. 我可以插入新的联系人,但是更新完全没有作用。

Here is the code I use: 这是我使用的代码:

public void update(Relation r)
{
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    // Name
    Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, r.getBook_id());
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, r.getFirstName()+ " " +r.getLastName());
    ops.add(builder.build());

    // Number
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, r.getBook_id());
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, r.getNumber());
    builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
    ops.add(builder.build());

    // Picture
    try
    {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(MyApplication.getInstance().getContentResolver(), Uri.parse(r.getPhoto()));
        ByteArrayOutputStream image = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, r.getBook_id());
        builder.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
        ops.add(builder.build());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    // Update
    try
    {
        MyApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

The one above doesn't work but this one does (Insert): 上面的一个不起作用,但是这个可以(插入):

public void insert(Relation r)
{
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int rawContactInsertIndex = ops.size();

    Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
    builder.withValue(RawContacts.ACCOUNT_TYPE, null);
    builder.withValue(RawContacts.ACCOUNT_NAME, null);
    ops.add(builder.build());

    // Name
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, r.getFirstName()+ " " +r.getLastName());
    ops.add(builder.build());

    // Number
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, r.getNumber());
    builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
    ops.add(builder.build());

    // Picture
    try
    {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(MyApplication.getInstance().getContentResolver(), Uri.parse(r.getPhoto()));
        ByteArrayOutputStream image = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);
        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex);
        builder.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray());
        ops.add(builder.build());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    // Add the new contact
    ContentProviderResult[] res;
    try
    {
        res = MyApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        if (res != null && res[0] != null)
        {
            String uri = res[0].uri.getPath().substring(14);
            r.setBook_id(new Integer(uri).intValue());
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Can you tell me if I'm doing it in the good way or not? 你能告诉我我是否做得很好吗?

Thanks. 谢谢。

Regards. 问候。

V. V.

Why did you use newInsert in your update method? 为什么在更新方法中使用newInsert? Why not newUpdate? 为什么不使用newUpdate? Maybe it's a mistake. 也许是个错误。 Please check your code again, i want refer to your code. 请再次检查您的代码,我想参考您的代码。

I have had a same problem with update name for a contact,although it doesn't throw any exception, i can't find problem was from where. 我对联系人的更新名称也遇到了同样的问题,尽管它没有引发任何异常,但是我找不到问题出在哪里。

Solved. 解决了。 Check previous message. 检查上一条消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM