Android添加联系人有两种方式:

1. 直接调用插入语句,先插入一个空Item,得到一个id,然后给这个id对应的插入其他信息,如姓名,号码,邮件等;

2. 利用ContentProviderOperation, 在系统应用Phonebook中就是采用这种方式, 代码如下:

实现了插入姓名,号码以及头像。

     private boolean saveUpdatedPhoto(long rawContactId, File photoFile) {
final Uri outputUri = Uri.withAppendedPath(ContentUris.withAppendedId(
RawContacts.CONTENT_URI, rawContactId),
RawContacts.DisplayPhoto.CONTENT_DIRECTORY);
boolean result = false;
try {
final FileOutputStream outputStream = getContentResolver()
.openAssetFileDescriptor(outputUri, "rw")
.createOutputStream();
try {
final FileInputStream inputStream = new FileInputStream(
photoFile);
try {
final byte[] buffer = new byte[16 * 1024];
int length;
int totalLength = 0;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
totalLength += length;
}
result = true;
} finally {
inputStream.close();
}
} finally {
outputStream.close();
photoFile.delete();
}
} catch (IOException e) {
result = false;
}
return result;
} public void addContact(String name, CharSequence phoneNumber1,
CharSequence phoneNumber2, String email1, String email2, String im,
String nickname) {
final ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
ContentProviderOperation.Builder builder = null;
int rawContactIndex = 0; builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
builder.withValue(RawContacts.ACCOUNT_NAME, "Phone contacts");
builder.withValue(RawContacts.ACCOUNT_TYPE,
"com.sonyericsson.localcontacts");
builder.withValue(RawContacts.AGGREGATION_MODE,
RawContacts.AGGREGATION_MODE_DISABLED);
operationList.add(builder.build()); // Add name.
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(StructuredName.RAW_CONTACT_ID,
rawContactIndex);
builder.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(StructuredName.GIVEN_NAME, name);
operationList.add(builder.build()); // Add phone number.
if (!TextUtils.isEmpty(phoneNumber1)) {
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Phone.RAW_CONTACT_ID,
rawContactIndex);
builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
builder.withValue(Phone.TYPE, Phone.TYPE_MOBILE);
builder.withValue(Phone.NUMBER, phoneNumber1);
builder.withValue(Data.IS_PRIMARY, 1);
operationList.add(builder.build());
} // Add phone number.
if (!TextUtils.isEmpty(phoneNumber2)) {
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Phone.RAW_CONTACT_ID,
rawContactIndex);
builder.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
builder.withValue(Phone.TYPE, Phone.TYPE_HOME);
builder.withValue(Phone.NUMBER, phoneNumber2);
builder.withValue(Data.IS_PRIMARY, 0);
operationList.add(builder.build());
} // Add Email info.
if (!TextUtils.isEmpty(email1)) {
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Email.RAW_CONTACT_ID,
rawContactIndex);
builder.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
builder.withValue(Email.TYPE, Email.TYPE_MOBILE);
builder.withValue(Email.DATA, email1);
operationList.add(builder.build());
} if (!TextUtils.isEmpty(email2)) {
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Email.RAW_CONTACT_ID,
rawContactIndex);
builder.withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
builder.withValue(Email.TYPE, Email.TYPE_HOME);
builder.withValue(Email.DATA, email2);
operationList.add(builder.build());
} // Add IM info.
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Im.RAW_CONTACT_ID, rawContactIndex);
builder.withValue(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
builder.withValue(Im.TYPE, Im.PROTOCOL_QQ);
builder.withValue(Im.DATA, im);
operationList.add(builder.build()); // Add Nickname info.
builder = ContentProviderOperation.newInsert(Data.CONTENT_URI);
builder.withValueBackReference(Nickname.RAW_CONTACT_ID, rawContactIndex);
builder.withValue(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE);
builder.withValue(Nickname.TYPE, Nickname.TYPE_INITIALS);
builder.withValue(Nickname.DATA, nickname);
operationList.add(builder.build()); try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY,
operationList);
// Get raw_contact_id
Cursor cc = getContentResolver().query(RawContacts.CONTENT_URI,
null, "display_name=?", new String[] { name }, null);
cc.moveToFirst();
// Insert photo for the contact.
saveUpdatedPhoto(cc.getLong(cc.getColumnIndex("_id")), new File(
"/sdcard/DCIM/Camera/ContactPhoto-IMG_19700104_013154.jpg"));
} catch (RemoteException e) {
} catch (OperationApplicationException e) {
}
}

基本思路是先插入除头像以外的其他信息,然后获得raw_contact_id,再给这个id插入头像,具体方法是saveUpdatedPhoto()。

Note:本人不太清楚能不能像插入姓名一样插入头像,所以才想出了这么一招,如果谁有更好的方法,不需要查询获得id就可以直接插入头像,共享一哈。

05-06 19:54