i need add contact automatically in phone book app....i found link..
how add new contacts in android
using this, able add phone no.,name,email etc. don't know how add photo.
firstly convert image bitmap , use below code adding photo contact.
onclicklistener addclicklistener = new onclicklistener() { @override public void onclick(view v) { // getting reference name edittext edittext etname = (edittext) findviewbyid(r.id.et_name); // getting reference mobile edittext edittext etmobile = (edittext) findviewbyid(r.id.et_mobile); arraylist<contentprovideroperation> ops = new arraylist<contentprovideroperation>(); int rawcontactid = ops.size(); // adding insert operation operations list // insert new raw contact in 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 operations list // insert display name in 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 operations list // insert mobile number in 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 image selected mbitmap.compress(bitmap.compressformat.png , 75, stream); // adding insert operation operations list // insert photo in 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 insert operations single database transaction getcontentresolver().applybatch(contactscontract.authority, ops); toast.maketext(getbasecontext(), "contact added", toast.length_short).show(); }catch (remoteexception e) { e.printstacktrace(); }catch (operationapplicationexception e) { e.printstacktrace(); } } };
for more details please refer below link http://wptrafficanalyzer.in/blog/programatically-adding-contacts-with-photo-using-contacts-provider-in-android-example/
Comments
Post a Comment