Programmatically added Contact is not visible in Android [duplicate]

故事扮演 提交于 2019-12-12 05:16:21

问题


Possible Duplicate:
How can I programmatically add a contact?

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.

I have followed this example in setting up my code.


回答1:


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();
    }
}

Usage:

int book_id = insert("Doe", "John", "111-222-333", "content://com.my.package/drawable/photo");

book_id is the row id of your entry.



来源:https://stackoverflow.com/questions/10276252/programmatically-added-contact-is-not-visible-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!