转帖: http://www.ophonesdn.com/forum/thread-4969-1-1.html
终于android 上存储联系人搞定了。由于2.0后存储联系人做了改动,所以存储方法是不一样的,现在贴出来:
(一)2.0以下
public class AddContactAPI3 {
public void addContact(Context context, String name, String organisation,
String phone, String fax, String email, String address,
String website, Bitmap logo) {
ContentValues personValues = new ContentValues();
personValues.put(Contacts.People.NAME, name);
personValues.put(Contacts.People.STARRED, 1);
Uri newPersonUri = context.getContentResolver().insert(
Contacts.People.CONTENT_URI, personValues);
if (newPersonUri != null) {
// add group
long personId = ContentUris.parseId(newPersonUri);
ContentValues group = new ContentValues();
group.put(GroupMembership.PERSON_ID, personId);
group.put(GroupMembership.GROUP_ID, 1);
Uri groupUpdate = context.getContentResolver().insert(
GroupMembership.CONTENT_URI, group);
// add company (organisation)
if (!AppUtils.isBlank(organisation)) {
ContentValues organisationValues = new ContentValues();
Uri orgUri = Uri.withAppendedPath(newPersonUri,
Contacts.Organizations.CONTENT_DIRECTORY);
organisationValues
.put(Contacts.Organizations.COMPANY, organisation);
organisationValues.put(Contacts.Organizations.TYPE,
Contacts.Organizations.TYPE_WORK);
Uri orgUpdate = context.getContentResolver().insert(orgUri,
organisationValues);
}
// add mobile phone number
if (!AppUtils.isBlank(phone)) {
ContentValues mobileValues = new ContentValues();
Uri mobileUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.Phones.CONTENT_DIRECTORY);
mobileValues.put(Contacts.Phones.NUMBER, phone);
mobileValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_MOBILE);
Uri phoneUpdate = context.getContentResolver().insert(mobileUri,
mobileValues);
}
// add fax number
if (!AppUtils.isBlank(fax)) {
ContentValues faxValues = new ContentValues();
Uri faxUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.Phones.CONTENT_DIRECTORY);
faxValues.put(Contacts.Phones.NUMBER, fax);
faxValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_FAX_WORK);
Uri phoneUpdate = context.getContentResolver().insert(faxUri, faxValues);
}
// add email
if (!AppUtils.isBlank(email)) {
ContentValues emailValues = new ContentValues();
Uri emailUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.ContactMethods.CONTENT_DIRECTORY);
emailValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);
emailValues.put(Contacts.ContactMethods.TYPE,
Contacts.ContactMethods.TYPE_HOME);
emailValues.put(Contacts.ContactMethods.DATA, email);
Uri emailUpdate = context.getContentResolver()
.insert(emailUri, emailValues);
}
// add address
if (!AppUtils.isBlank(address)) {
ContentValues addressValues = new ContentValues();
Uri addressUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.ContactMethods.CONTENT_DIRECTORY);
addressValues.put(Contacts.ContactMethods.KIND,
Contacts.KIND_POSTAL);
addressValues.put(Contacts.ContactMethods.TYPE,
Contacts.ContactMethods.TYPE_HOME);
addressValues.put(Contacts.ContactMethods.DATA, address);
Uri addressUpdate = context.getContentResolver().insert(addressUri,
addressValues);
}
// add logo image
Bitmap bm = logo;
if (bm != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] photo = baos.toByteArray();
if (photo != null) {
People.setPhotoData(context.getContentResolver(), newPersonUri,
photo);
}
}
}
}
}
(二)2.0以上可以批量存储
public class AddContactAPI5 {
public void addContact(Context context, String name, String organisation,
String phone, String fax, String email, String address,
String website, Bitmap logo) {
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED)
.build());
// add name
if (!AppUtils.isBlank(name)) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
}
// add company
if (!AppUtils.isBlank(organisation)) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,
organisation)
.withValue(ContactsContract.CommonDataKinds.Organization.TYPE,
ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
.build());
}
// add phone
if (!AppUtils.isBlank(phone)) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 1)
.build());
}
// add Fax
if (!AppUtils.isBlank(fax)) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, fax)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK)
.build());
}
// add email
if (!AppUtils.isBlank(email)) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE, 1)
.build());
}
// add address
if (!AppUtils.isBlank(address)) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, address)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
.build());
}
// add website
if (!AppUtils.isBlank(website)) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Website.URL, website)
.withValue(ContactsContract.CommonDataKinds.Website.TYPE,
ContactsContract.CommonDataKinds.Website.TYPE_WORK)
.build());
}
// add logo image
Bitmap bm = logo;
if (bm != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] photo = baos.toByteArray();
if (photo != null) {
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.PHOTO, photo)
.build());
}
}
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}
}
}
(三)将以上2种方法中比较关键的东西提出来:
1)2.0以下存储到固定的group里,这个调查了好久才高出来:
long personId = ContentUris.parseId(newPersonUri);
ContentValues group = new ContentValues();
group.put(GroupMembership.PERSON_ID, personId);
group.put(GroupMembership.GROUP_ID, 1);
Uri groupUpdate = context.getContentResolver().insert(
GroupMembership.CONTENT_URI, group);
2)2.0以下存储联系人图片:
Bitmap bm = logo;
if (bm != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] photo = baos.toByteArray();
if (photo != null) {
People.setPhotoData(context.getContentResolver(), newPersonUri,
photo);
}
3)2.0以上存储联系人图片方法:
Bitmap bm = logo;
if (bm != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] photo = baos.toByteArray();
if (photo != null) {
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.PHOTO, photo)
.build());
}
4)2.0以上至关重要的一点:
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED)
加上这句才会把同名联系人分开存储,否则一条联系人调用2次存储后,在对联系人进行edit的时候后报错。这条属性在官方API上都没有说明,很重要。
(四)当然不管1.5还是2.0都可以通过intent调用到系统的edit contact:
Intent it = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
it.putExtra(android.provider.Contacts.Intents.Insert.NAME, firstName);
it.putExtra(android.provider.Contacts.Intents.Insert.COMPANY, organization);
it.putExtra(android.provider.Contacts.Intents.Insert.JOB_TITLE, title);
it.putExtra(android.provider.Contacts.Intents.Insert.EMAIL, workEmail);
it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_EMAIL, homeEmail);
it.putExtra(android.provider.Contacts.Intents.Insert.PHONE, mobilePhone);
it.putExtra(android.provider.Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE);
it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE, homePhone);
it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE_TYPE,
Contacts.PhonesColumns.TYPE_HOME);
it.putExtra(android.provider.Contacts.Intents.Insert.TERTIARY_PHONE, workPhone);
it.putExtra(android.provider.Contacts.Intents.Insert.TERTIARY_PHONE_TYPE,
Contacts.PhonesColumns.TYPE_WORK);
it.putExtra(android.provider.Contacts.Intents.Insert.POSTAL, homeAddress);
startActivity(it);
转帖: http://www.ophonesdn.com/forum/thread-4969-1-1.html