0

I have been stuck on this problem for days, hoping someone can help. I am trying to update a contacts photo with a specific image using the CONTACT_ID, but this doesn't appear to be working. Do I have to use RAW_CONTACT_ID in order to do this? Because ideally I would like to apply the same photo to all raw contact components of the same contact id. Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {

//set up contact list
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter (this, android.R.layout.simple_list_item_2,
            null, new String[] {Contacts.DISPLAY_NAME}, new int[] {android.R.id.text1}, 0);
ListView contactList = (ListView)findViewById(android.R.id.list);
contactList.setAdapter(mAdapter);

//set up intents for when a contact is selected
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.d(TAG, "action pick");
startActivityForResult(intent, PICK_CONTACT);
} 

@Override
protected void onActivityResult (int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    super.onResume();

    switch (reqCode) {
    case (PICK_CONTACT):
        if (resultCode == Activity.RESULT_OK) {
            contactData = data.getData();
            //get _ID (contact id based on first column in contact database
            if (contactData != null) {
                Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
                if (cursor!=null && cursor.moveToNext()) {
                    contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data._ID));
                    cursor.close();

                }
            }

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.woman);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.PNG, 0, stream);
            byte[] imageBytes = stream.toByteArray();   

             ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
             ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(ContactsContract.Data._ID + " = ?", new String[] 
                        {contactId}).withValue(ContactsContract.Data.DATA15,imageBytes).build());

             try {
                cr.applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    //view contact
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);
    intent.setData(uri);
    startActivity(intent);
    break;
    }

}

I tried using rawContactUri, but I don't think this is the same as the RAW_CONTACT_ID. Does anyone have any suggestions on how to do this?

ash
  • 11
  • 4

1 Answers1

0

Looking into this, it seems that the PHOTO_ID is read-only (http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html), which suggests that I cant change it through the program. It seems that other people have had success doing it, so if anyone finds a way let me know!

ash
  • 11
  • 4