title: How to set a photo in android via the contacts api
body: <p>i'm new to developing android apps and I'm currently trying to do the following:
  Search all contacts' notes for a certain string (say: "E-Plus") and, if found, place a certain icon (say the E-Plus logo) as their contact photo.</p>
  
  <p>I got it working using eclipse and the following test AVD:
  Platform: Android 2.3.3
  API Level: 10
  CPU/ABI: ARM (armeabi)</p>
  
  <p>Now, when i copy the *.apk file generated from eclipse to my Samsung Galaxy S2, the app only works partly.
  I added a line to display a toast after searching all contacts, that tells me how many contacts have been found with the specific string. This number is correct, when i execute the app on my actual Galaxy S2.</p>
  
  <p>But in contrary to the testing environment, the app doesnt set the photo afterwards. The app also doesn't crash.
  Then i thought, setting a photo might be too difficult as a first approach, so i also tried adding a phone number - same result.
  The phone number is added correctly in the testing environment, but not on my actual phone.</p>
  
  <p>I tried both a google contact on my phone and a normal phonebook entry contact to have the certain note in its details.
  Both were found correctly by the app (that is note was read and string was found) but neither of them had the photo or phone number set after running the app.</p>
  
  <p>Here is what i do:</p>
  
  <p>The following code should load all contacts and save their id and note. Note that ContactInfo is just a simple class with variables to hold the contactId etc. (String contactId, String note, Byte[] Photo) and according get and set methods.</p>
  
  <pre><code>public ArrayList&lt;ContactInfo&gt; loadAllContacts(ContentResolver cr) {
      ArrayList&lt;ContactInfo&gt; contactList = new ArrayList&lt;ContactInfo&gt;();
  
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
          if (cur.getCount() &gt; 0) {
              while (cur.moveToNext()) {
                  String contactId = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  ContactInfo contactInfo = new ContactInfo(contactId);
  
                  // Get Note
                      String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
                      String[] whereParams = new String[]{contactId,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
                              Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParams, null); 
                      if (noteCur.moveToFirst()) { 
                          String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                          contactInfo.setNote(note);
                      } 
                      noteCur.close();                        
  
                  contactList.add(contactInfo);                   
              }
          }
  
      return contactList;
  }
  </code></pre>
  
  <p>The following code should update all contacts setting a photo (and for further testing, a phone number).
  The setPhoto method I got from the following blog: <a href="http://thinkandroid.wordpress.com/2009/12/30/handling-contact-photos-all-api-levels/" rel="nofollow">Handling Contact Photos</a> and because it worked perfectly in the testing AVD I thought that theres probably no error in it.</p>
  
  <pre><code>public void updateAllContacts(ContentResolver cr, ArrayList&lt;ContactInfo&gt; contactList) {
      while (!contactList.isEmpty()) {
          ContactInfo contact = contactList.remove(0);
          String contactId = contact.getContactId();
  
          setPhoto(cr,contact.getPhoto(),contactId);
          setPhoneNumber(cr,contact.getPhoneNumber(),contactId);
      }
  
  }
  
  public void setPhoneNumber(ContentResolver cr, String phoneNumber, String contactId) {
      ContentValues values = new ContentValues();
  
      values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
      values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
      values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,phoneNumber);
      values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_CUSTOM);
  
      cr.insert(ContactsContract.Data.CONTENT_URI, values);
  }
  
  public void setPhoto(ContentResolver cr, byte[] bytes, String contactId) {      
      ContentValues values = new ContentValues();
      int photoRow = -1;
      String where = ContactsContract.Data.RAW_CONTACT_ID + " = " + contactId + " AND " + ContactsContract.Data.MIMETYPE + "=='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
      Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
      int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
      if (cursor.moveToFirst()) {
          photoRow = cursor.getInt(idIdx);
      }
      cursor.close();
  
      values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
      values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
      values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);
      values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
  
      if (photoRow &gt;= 0) {
          cr.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID + " = " + photoRow, null);
      } else {
          cr.insert(ContactsContract.Data.CONTENT_URI, values);
      }
  }
  </code></pre>
  
  <p>The following routine I use to get the byte array from the *.png in the res folder of my app.</p>
  
  <pre><code>public static byte[] getByteArrayFromResource(Resources rsrc,int id) {
      Bitmap bmp = BitmapFactory.decodeResource(rsrc,id);
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] bytes = stream.toByteArray();
  
      return bytes;
  }
  </code></pre>
  
  <p>Finally, what I execute in my main routine, is:</p>
  
  <pre><code>ArrayList&lt;ContactInfo&gt; contactList = new ArrayList&lt;ContactInfo&gt;();
  ContactsAccessor contactsAccessor = new ContactsAccessor();
  contactList = contactsAccessor.loadAllContacts(getContentResolver());
  
  int test = 0;
  for (int i=0; i&lt;contactList.size(); i++) {
       ContactInfo contact = contactList.get(i);
       String note = contact.getNote();
  
       if (note.contains("TESTSHRAZAAM")) {                       
          contact.setPhoneNumber("12345");
          contact.setPhoto(ImageHandler.getByteArrayFromResource(getResources(),R.drawable.ic_eplus));
          test = test+1;
       }
   }
  
   contactsAccessor.updateAllContacts(getContentResolver(), contactList);
  
   if (test&gt;0) Toast.makeText(WelchesNetzIcons.this,"Es wurden "+test+" Icons gesetzt.", Toast.LENGTH_SHORT).show();
   else Toast.makeText(WelchesNetzIcons.this, "Es wurden keine Icons gesetzt.", Toast.LENGTH_SHORT).show();
  </code></pre>
  
  <p>I geatly appreciate any suggestions on what I might be doing wrong here. Also, I tried to be as precise as I could, but if you are interested in or need any further information, please don't hesitate to ask.</p>
  
  <p>Thank you very much for your effort in helping me.</p>
  
  <p>Best regards, Niklas</p>