1

我正在开发一个安卓应用程序。在我的应用程序中,我必须向 Android 联系人添加详细信息和图像。所以我使用了以下代码

private void addcontact() {
            // TODO Auto-generated method stub

            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
            int rawContactInsertIndex = ops.size();
            ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
               .withValue(RawContacts.ACCOUNT_TYPE, null)
               .withValue(RawContacts.ACCOUNT_NAME,null )
               .build());
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
               .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
               .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
               .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
               .build());

            Bitmap bm = getBitmapFromURL(imageurl);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.PNG , 75, stream);

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValue(ContactsContract.Data.RAW_CONTACT_ID, 9) // here 9 is _ID where I'm inserting image
                       .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                       .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
                       .build());



            ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
               .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
               .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
               .withValue(StructuredName.DISPLAY_NAME, "sarath")
               .build());



            try {
                ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        }
    });

代码正常工作。联系人已成功添加到联系人,但未加载联系人图像。我的第二个疑问是我有 3 种类型的 phoen 号码(家庭、工作、手机)。我可以添加一个电话号码,但如何添加所有三个。如何添加地址和电子邮件ID..请朋友帮我

4

1 回答 1

2

最后我得到了我的问题的答案所以我在这里发帖。可能对某人有帮助。

private void addcontact(String name, String phone, String profilepic) {
    // TODO Auto-generated method stub



    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int rawContactInsertIndex = ops.size();

    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_TYPE, null)
            .withValue(RawContacts.ACCOUNT_NAME, null).build());
    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
                    rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.NUMBER, phone).build());

    Drawable dr = getResources().getDrawable(R.drawable.ic_launcher);

    Bitmap bm = getBitmapFromURL(profilepic);

    // bm = BitmapFactory.decodeResource(getResources(),
    // R.drawable.ic_launcher);
    //

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 80, baos);
    byte[] b = baos.toByteArray();

    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(
                    ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Photo.DATA15, b)
            .build());

    // ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // bm.compress(Bitmap.CompressFormat.PNG , 75, stream);
    //
    // ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
    // .withValue(ContactsContract.Data.RAW_CONTACT_ID, 9) // here 9 is _ID
    // where I'm inserting image
    // .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
    // .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
    // .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
    // .build());

    ops.add(ContentProviderOperation
            .newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID,
                    rawContactInsertIndex)
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.DISPLAY_NAME, name).build());

    try {
        ContentProviderResult[] res = getContentResolver().applyBatch(
                ContactsContract.AUTHORITY, ops);

        Toast.makeText(getApplicationContext(),
                "contact added to the phone", Toast.LENGTH_LONG).show();
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
于 2013-09-11T09:32:36.463 回答