Hi,
I'm trying to change chosen contact's photo and I ran into some
trouble..
I used the code from this post:
http://groups.google.com/group/android-developers/msg/7798b51e01c61c1e?
and this is my entire code:
public class ContactFacesActivity extends ListActivity {
ArrayList<Contact> Contacts;
ContentResolver cr;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Contacts = new ArrayList<Contact>();
cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id =
cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name =
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if
(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
> 0) {
Contacts.add(new Contact(name, id));
}
}
setListAdapter(new
ArrayAdapter<Contact>(ContactFacesActivity.this,
android.R.layout.simple_list_item_1, Contacts));
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long
id) {
super.onListItemClick(l, v, position, id);
Bitmap temp =
((BitmapDrawable)getResources().getDrawable(R.drawable.house)).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
temp.compress(CompressFormat.JPEG, 90 , bos);
byte[] bitmapdata = bos.toByteArray();
setPhoto(Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_URI,
Contacts.get(position).getID()), bitmapdata);
}
public void setPhoto(Uri personUri, byte[] photo) {
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID
+ " == " +
ContentUris.parseId(personUri) + " AND " +
Data.MIMETYPE + "=='" +
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = getContentResolver().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,
ContentUris.parseId(personUri));
values.put(ContactsContract.Data.IS_SUPER_PRIMARY,
1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO,
photo);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if(photoRow >= 0){
getContentResolver().update
(ContactsContract.Data.CONTENT_URI, values,
ContactsContract.Data._ID
+ " = " + photoRow, null);
} else {
getContentResolver().insert
(ContactsContract.Data.CONTENT_URI, values);
}
}
}
But it doesn't seem to work... Am I doing anything wrong?
Thanks :)
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en