79323

Programmatically added Contact is not visible in Android [duplicate]

Question:

<blockquote>

<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/3726282/how-can-i-programmatically-add-a-contact" rel="nofollow">How can I programmatically add a contact?</a>

</blockquote>

With the help of a Google search I was able to add a Contact in my Android Application. While this works, I am not able to see that added Contact in the phone Contact List (Phonebook).

Could you help me fix this? I don't know where to look, is it perhaps a versioning problem? I would be grateful if anyone who has encountered this problem would care to help me out.

I have followed <a href="http://saigeethamn.blogspot.in/2011/05/contacts-api-20-and-above-android.html" rel="nofollow">this example</a> in setting up my code.

Answer1:

Insert a new contact into your phone book with the following method:

public void insert(String lastName, String firstName, String phoneNumber, String photo_uri) { ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI); builder.withValue(RawContacts.ACCOUNT_TYPE, null); builder.withValue(RawContacts.ACCOUNT_NAME, null); ops.add(builder.build()); // Name builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI); builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName); builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName); ops.add(builder.build()); // Number builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI); builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); builder.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber); builder.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME); ops.add(builder.build()); // Picture try { Bitmap mBitmap = Media.getBitmap(context.getContentResolver(), Uri.parse(photo_uri)); ByteArrayOutputStream image = new ByteArrayOutputStream(); mBitmap.compress(Bitmap.CompressFormat.JPEG , 100, image); builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI); builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0); builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, image.toByteArray()); ops.add(builder.build()); } catch (Exception e) { e.printStackTrace(); } // Add the new contact ContentProviderResult[] res; try { res = KramerApplication.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); if (res != null && res[0] != null) { String uri = res[0].uri.getPath().substring(14); return new Integer(uri).intValue(); // Book ID } } catch (Exception e) { e.printStackTrace(); } }

Usage:

int book_id = insert("Doe", "John", "111-222-333", "content://com.my.package/drawable/photo");

book_id is the row id of your entry.

Recommend

  • kitkat (API 19) inserting contact?
  • Multiple rows insert with ContentProvider
  • Not able to insert address of contacts in android
  • How is the value of 'auto_now_add' determined in ndb's async operation?
  • Performace gains of searching with FTS over it with LIKE on indexed colum(s)?
  • MVC 6 - TagHelper Select
  • Delete contact from android contacts
  • How I can read an item through keys in firebase?
  • Table row splits across two pages (print media)
  • how do you “Press ENTER to continue, anything else to quit” in C++
  • The binary operator LessThan is not defined for the types 'System.Nullable`1[System.DateTime]&#
  • Is Android capable of managing microphone/input volume?
  • Instead of adding another element It is landing to a another page
  • Relational Database Mapping in MVC
  • Relay Error when deleting: RelayMutationQuery: Invalid field name on fat query
  • Multi-line JSON read using Apache PIG
  • Store additional information during registration with Firebase in Android
  • sql if statement checking for null parameter
  • Can I customize a Jackson ObjectMapper by adding a module?
  • Trying to get all column names in a table
  • JSF Cannot format given Object as a Date
  • How to set the navigation bar to the top in Table View?
  • FlexJSON Orders Alphabetically by Default
  • UIPickerView without IB?
  • LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`2[System.
  • How to add learning rate to summaries?
  • Cassandra NoClassDefFoundError: com/google/common/util/concurrent/AsyncFunction
  • Strong vs Weak entities MYSQL
  • SqlCommand back up Database
  • Bad request using file_get_contents for PUT request in PHP
  • Disable Enter in editText android
  • Dynamically accessing properties of knockoutjs observable array
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal