title: Adding new contact with photo via ContactsContract and ContentProvider
body: <p>I've been trying all day to add contact with photo to Android Contacts. Finally I'm adding picture to contact, but I can't add any name or phone number. Contact is displaying as "(Unknown)", with proper photo.</p>
  
  <p>My code:</p>
  
  <pre><code>private void addContact2() {
  final String displayName = "XYZA";
  final String mobileNumber = "666666";
  final byte[] photoByteArray; // initalized elsewhere
  
  ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;&gt;();
  
  ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
          .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "")
          .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "")
          .build());
  
  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
          .withValue(ContactsContract.Data.RAW_CONTACT_ID, 0)
          .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
          .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
          //.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, displayName)
          .build());
  
  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, photoByteArray)
          .build());
  
  Uri newContactUri = null;
  ContentProviderResult[] res = null;
  try {
      final ContentResolver contentResolver = getContentResolver();
      res = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
      if (res != null &amp;&amp; res.length &gt; 0 &amp;&amp; res[0] != null) {
          newContactUri = res[0].uri;
          Log.d(CallActivity.class.getName(), "URI added contact:"+ newContactUri);
          Toast.makeText(this, "Successfully added " + displayName, Toast.LENGTH_LONG).show();
      }
      else Log.e(CallActivity.class.getName(), "Contact not added.");
  } catch (NullPointerException | RemoteException | OperationApplicationException e) {
      Log.e(CallActivity.class.getName(), e.getMessage(), e);
  }
  }
  </code></pre>