[android-developers] Re: Trouble setting contact photos with ContactsContract
Well, I found a simpler way, which seems to work. You need to know
that the photo lives in the extension data for a contact, so you are
just adding a row of extension data with the right contact ID.
Here's adding a photo:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.RAW_CONTACT_ID, personId);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO,
bytes.toByteArray());
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE );
context.getContentResolver().update
(ContactsContract.Data.CONTENT_URI, values,
ContactsContract.Data.RAW_CONTACT_ID + " = " + personId, null);
And here is removing one:
context.getContentResolver().delete
(ContactsContract.Data.CONTENT_URI,
ContactsContract.Data.RAW_CONTACT_ID + " = " + String.valueOf
(personId) +
" AND " + ContactsContract.Data.MIMETYPE + " = " + "\"" +
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE+ "\"",
null);
Richard
On Nov 3, 11:47 am, jarkman <jark...@gmail.com> wrote:
> I'm happily retrieving contact photos with the new ContactsContract
> APIs and cursor.getBlob, but I'm having great trouble working out how
> to set a contact photo.
>
> I believe I have the right URI (at least, it works for retrieving the
> photo), but it just won't work for updating or adding one.
>
> Here's my best bet:
>
> ByteArrayOutputStream bytes = new ByteArrayOutputStream();
> bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
>
> Uri uri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_URI,
> friendId );
> uri = Uri.withAppendedPath( uri,
> ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
>
> // eg: content://com.android.contacts/contacts/6/photo
>
> ContentValues values = new ContentValues();
>
> values.put(ContactsContract.CommonDataKinds.Photo.PHOTO,
> bytes.toByteArray());
> values.put(ContactsContract.CommonDataKinds.Photo.MIMETYPE,
> ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE );
>
> context.getContentResolver().insert(uri, values);
>
> And here's my exception:
>
> java.lang.UnsupportedOperationException: Unknown uri:
> content://com.android.contacts/contacts/6/photo
> at com.android.providers.contacts.LegacyApiSupport.update
> (LegacyApiSupport.java:914)
> at
> com.android.providers.contacts.ContactsProvider2.updateInTransaction
> (ContactsProvider2.java:2924)
> at com.android.providers.contacts.SQLiteContentProvider.update
> (SQLiteContentProvider.java:139)
> at com.android.providers.contacts.ContactsProvider2.update
> (ContactsProvider2.java:1923)
> at android.content.ContentProvider$Transport.update
> (ContentProvider.java:180)
> at android.content.ContentProviderNative.onTransact
> (ContentProviderNative.java:195)
> at android.os.Binder.execTransact(Binder.java:287)
> at dalvik.system.NativeStart.run(Native Method)
>
> Any thoughts ?
>
> Thanks,
>
> Richard
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home