在Android应用程序的电话簿中自动添加联系人

时间:2016-01-29 06:09:50

标签: android contact

我需要通过我的应用程序在电话簿中自动添加联系人....我找到了这个链接..

How to add new contacts in android

使用此功能,我可以添加电话号码,姓名,电子邮件等。但不知道如何添加照片。

2 个答案:

答案 0 :(得分:0)

首先将图片转换为位图,然后使用下面的代码将照片添加到您的联系人。

OnClickListener addClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {

                // Getting reference to Name EditText
                EditText etName = (EditText) findViewById(R.id.et_name);

                // Getting reference to Mobile EditText
                EditText etMobile = (EditText) findViewById(R.id.et_mobile);

                ArrayList<ContentProviderOperation> ops =
                        new ArrayList<ContentProviderOperation>();

                int rawContactID = ops.size();

                // Adding insert operation to operations list
                // to insert a new raw contact in the table ContactsContract.RawContacts
                ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                        .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                        .withValue(RawContacts.ACCOUNT_NAME, null)
                        .build());

                // Adding insert operation to operations list
                // to insert display name in the table ContactsContract.Data
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(StructuredName.DISPLAY_NAME, etName.getText().toString())
                        .build());

                // Adding insert operation to operations list
                // to insert Mobile Number in the table ContactsContract.Data
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                        .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                        .withValue(Phone.NUMBER, etMobile.getText().toString())
                        .withValue(Phone.TYPE, CommonDataKinds.Phone.TYPE_MOBILE)
                        .build());

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                if(mBitmap!=null){    // If an image is selected successfully
                    mBitmap.compress(Bitmap.CompressFormat.PNG , 75, stream);

                    // Adding insert operation to operations list
                    // to insert Photo in the table ContactsContract.Data
                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
                            .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                            .withValue(ContactsContract.Data.MIMETYPE,Photo.CONTENT_ITEM_TYPE)
                            .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
                            .build());

                    try {
                        stream.flush();
                    }catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try{
                    // Executing all the insert operations as a single database transaction
                    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
                    Toast.makeText(getBaseContext(), "Contact is successfully added", Toast.LENGTH_SHORT).show();
                }catch (RemoteException e) {
                    e.printStackTrace();
                }catch (OperationApplicationException e) {
                    e.printStackTrace();
                }
            }
        };

有关详细信息,请参阅以下链接 http://wptrafficanalyzer.in/blog/programatically-adding-contacts-with-photo-using-contacts-provider-in-android-example/

答案 1 :(得分:0)

使用以下代码将此图片作为方法

的photoByteArray
 ALTER proc [dbo].[prCompanyAgentUpdate]
 (
 @NewLicenceNumber nvarchar(50),
 @NewDateIssued datetime,
 @NewExpirationDate datetime,
 @LicenceNumber nvarchar(50),
 @DateIssued datetime,
 @ExpirationDate datetime,
 @StateIssuedLicenceNumber nvarchar(50),
 @StateIssuedDateIssued datetime,
 @StateIssuedExpirationDate datetime
 )
 as

 Declare @CDateIssued datetime;
 Declare @CExpirationDate datetime; 
 Declare @CStateIssuedDateIssued datetime;
 Declare @CStateIssuedExpirationDate datetime;

 Set @CDateIssued= CONVERT(char(10),@DateIssued, 126);
 Set @CExpirationDate= CONVERT(char(10), @CExpirationDate, 126);
 Set @CStateIssuedDateIssued= CONVERT(char(10), @StateIssuedDateIssued, 126);
 Set @CStateIssuedExpirationDate= CONVERT(char(10), @StateIssuedExpirationDate, 126);

  UPDATE  [tbCompanyAgent]
  SET 
  LicenceNumber = @NewLicenceNumber,
  LicenceIssueDate = @NewDateIssued,
  LicenceExpirationDate =@NewExpirationDate
  WHERE
  LicenceNumber in (select distinct LicenceNumber 
  from tbLicence where LicenceNumber= @LicenceNumber AND          StateIssuedLicenseNumber= @StateIssuedLicenceNumber
  AND LicenceIssueDate = @CDateIssued AND StateIssuedLicenseIssuedDate =    @CStateIssuedDateIssued AND 
 StateIssuedLicenseExpirationDate = @CStateIssuedExpirationDate)