将照片添加到联系人 [英] Add a photo to a contact
问题描述
我正在尝试在电话通讯录中添加联系人。
我成功了:我添加了一个新联系人,并为其分配了一个手机号码。
I am trying to add a contact to the phone address book.
I have been successful: I added a new contact and assigned a mobile number to it.
现在我需要将我在资源目录中的JPG添加到联系人作为联系人照片。
我正在寻找一个教程,但找不到任何教程。
Now I need add a JPG I have in my resources directory to the contact as the contact photo.
I am looking for a tutorial, but can't find any.
我需要定位旧手机,因此我需要使用旧的联系人API。
I need to target old phones, so I need to use the old Contacts API.
任何人都可以帮忙吗?
ContentValues contact = new ContentValues();
contact.put(People.NAME, "testContact");
Uri insertUri = activity.getContentResolver().insert(People.CONTENT_URI, contact);
Uri phoneUri = Uri.withAppendedPath(insertUri, People.Phones.CONTENT_DIRECTORY);
contact.clear();
contact.put(People.Phones.TYPE, People.TYPE_MOBILE);
contact.put(People.NUMBER, "12128911");
updateUri = activity.getContentResolver().insert(phoneUri, contact);
推荐答案
我使用新的APi 8+,你也可以使用这个,(对于支持更低版本在清单文件中使用minSDKVersion你想要什么..)
I use new APi 8+, You can also use this, (For support lower version In manifest file use minSDKVersion what you want..)
我正在做的是什么,(我正在使用.PNG格式Bitmap )
And what I am doing is something like, (I am using .PNG format Bitmap)
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG , 75, stream);
operations.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());
try {
stream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这篇关于将照片添加到联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!