以编程方式添加联系不可见的Andr​​oid [英] Programmatically added Contact is not visible in Android

查看:125
本文介绍了以编程方式添加联系不可见的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  我如何以编程方式添加联系人?

通过谷歌搜索的帮助下,我能够在我的Andr​​oid应用程序中添加联系人。虽然这个作品,我不能够看到在手机联系人列表(电话簿)添加联系人。

With the help of a Google search I was able to add a Contact in my Android Application. While this works, I am not able to see that added Contact in the phone Contact List (Phonebook).

你能不能帮我解决这个问题?我不知道去哪里找,这是一个可能版本问题?我将不胜感激,如果谁遇到这个问题的人会关心帮助我。

Could you help me fix this? I don't know where to look, is it perhaps a versioning problem? I would be grateful if anyone who has encountered this problem would care to help me out.

我按照这个例子在建立我的code。

I have followed this example in setting up my code.

推荐答案

插入一个新的联系人到手机书下面的方法:

Insert a new contact into your phone book with the following method:

public void insert(String lastName, String firstName, String phoneNumber, String photo_uri)
{
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    Builder builder = ContentProviderOperation.newInsert(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, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName);
    ops.add(builder.build());

    // Number
    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber);
    builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
    ops.add(builder.build());

    // Picture
    try
    {
        Bitmap mBitmap = Media.getBitmap(context.getContentResolver(), Uri.parse(photo_uri));

        ByteArrayOutputStream image = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG , 100, image);

        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
        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 = KramerApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        if (res != null && res[0] != null)
        {
            String uri = res[0].uri.getPath().substring(14);
            return new Integer(uri).intValue(); // Book ID
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

用法:

INT book_id =插入(李四,约翰,111-222-333,内容://com.my.package/drawable/photo);

book_id 是项的行ID。

这篇关于以编程方式添加联系不可见的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆