Jake - thanks for that. Your code seems to match my understanding of Dmitri's recipe, and also actually seems to work for me. Which i hope is good news... :-)
Richard On Nov 5, 9:20 pm, "jak." <koda...@gmail.com> wrote: > Hi Dmitri & Jarkman, > Hope everyone's having a good day. > > I'm also working on adding photo's for a contact. Based on your > discussion I came up with this method. > It's seems to do the job, but I'm not entirely confident that I'm > doing things in the best way. > Can you please review it and let me know If I am fully grasping the > concept. I'm a little confused about when to use CONTACT_ID vs. > RAW_CONTACT_ID etc. > > Code below. Thanks a bunch! > Jake > > ------- > > public static void setPhoto(Uri personUri, byte[] photo, Context > context) { > 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 = context.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){ > context.getContentResolver().update > (ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data._ID > + " = " + photoRow, null); > } else { > context.getContentResolver().insert > (ContactsContract.Data.CONTENT_URI, values); > } > > } > > On Nov 5, 12:17 pm, Dmitri Plotnikov <dplotni...@google.com> wrote: > > > Right. > > > Setting the new photo to super-primary should take care of the PHOTO_ID > > reference. You should not need to worry about resetting the super-primary > > flag on other photos for the same contact - that's also automatic. > > > Cheers, > > - Dmitri > > > On Thu, Nov 5, 2009 at 12:04 PM, jarkman <jark...@gmail.com> wrote: > > > Oops! I see what you mean. Perhaps that update was a little > > > bold... :-) > > > > So I need to search out the right row (with the right contact ID and > > > the photo's MIME type) in the Data table with a query, then update > > > that row ? > > > > If there isn't a pre-existing photo, do I also need to update > > > ContactsContract.Data.PHOTO_ID on the contact after I have added a > > > photo ? > > > > Thanks! > > > > R. > > > > On Nov 5, 7:09 pm, Dmitri Plotnikov <dplotni...@google.com> wrote: > > > > This line: > > > > > context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, > > > > values, ContactsContract.Data.RAW_CONTACT_ID + " = " + personId, > > > > null); > > > > > attempts to change ALL data rows that have RAW_CONTACT_ID == personId, > > > which > > > > includes the photo, but also phone numbers, emails etc. You will need > > > > to > > > > find the specific row before updating it. The condition should say: > > > Data._ID > > > > + "=" + dataId. Alternatively, you can construct a specific URI: > > > > ContentUris.withAppendedId(Data.CONTENT_URI, dataId) > > > > > I hope this helps. > > > > - Dmitri > > > > > On Thu, Nov 5, 2009 at 9:05 AM, jarkman <jark...@gmail.com> wrote: > > > > > Dmitri - I wasn't, but I am now - thanks for the tip. > > > > > > It doesn't fix the problem, though. It seems as though I can set the > > > > > photo once on a brand-new contact, but I can never set it again on > > > > > that contact. > > > > > > Do I perhaps need to do something with > > > > > ContactsContract.Data.PHOTO_ID ? > > > > > > Here's code: > > > > > > ContentValues values = new ContentValues(); > > > > > values.put(ContactsContract.Data.RAW_CONTACT_ID, personId); > > > > > values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1); > > > > > 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's a stack: > > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): Writing exception to > > > > > parcel > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): > > > > > android.database.sqlite.SQLiteException: unknown error: Unable to > > > > > convert BLOB to string > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > android.database.CursorWindow.getString_native(Native Method) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > android.database.CursorWindow.getString(CursorWindow.java:329) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > android.database.AbstractWindowedCursor.getString > > > > > (AbstractWindowedCursor.java:49) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > com.android.providers.contacts.ContactsProvider2$DataRowHandler.getAugmente > > > dValues > > > > > (ContactsProvider2.java:1038) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > com.android.providers.contacts.ContactsProvider2$CommonDataRowHandler.updat > > > e > > > > > (ContactsProvider2.java:1225) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > com.android.providers.contacts.ContactsProvider2$PhoneDataRowHandler.update > > > > > (ContactsProvider2.java:1445) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > com.android.providers.contacts.ContactsProvider2.updateData > > > > > (ContactsProvider2.java:3091) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > com.android.providers.contacts.ContactsProvider2.updateData > > > > > (ContactsProvider2.java:3075) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > com.android.providers.contacts.ContactsProvider2.updateInTransaction > > > > > (ContactsProvider2.java:2854) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > com.android.providers.contacts.SQLiteContentProvider.update > > > > > (SQLiteContentProvider.java:139) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > com.android.providers.contacts.ContactsProvider2.update > > > > > (ContactsProvider2.java:1923) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > android.content.ContentProvider$Transport.update(ContentProvider.java: > > > > > 180) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > android.content.ContentProviderNative.onTransact > > > > > (ContentProviderNative.java:195) > > > > > 11-05 17:03:36.781: ERROR/DatabaseUtils(103): at > > > > > android.os.Binder.execTransact(Binder.java:287) > > > > > > On Nov 5, 4:41 pm, Dmitri Plotnikov <dplotni...@google.com> wrote: > > > > > > Hi Richard, > > > > > > > Regarding photos: are you setting the IS_SUPER_PRIMARY flag on your > > > Photo > > > > > > row? The notion of "super-primary" has to do with multiple > > > > > > accounts. > > > > > You > > > > > > can have only one super-primary photo for an entire aggregated > > > contact. > > > > > > > Cheers, > > > > > > - Dmitri > > > > > > > On Thu, Nov 5, 2009 at 1:51 AM, jarkman <jark...@gmail.com> wrote: > > > > > > > Jake - no, you are not the only one who suffers that frustration > > > with > > > > > > > the ContactsContract documentation. > > > > > > > > I've spent a lot of time writing frankly experimental code because > > > I > > > > > > > couldn't work out from the docs how the various URIs and string > > > > > > > constants had to be knitted together to actually work. I think the > > > > > > > docs really need a lot more tiny examples, to make the context > > > clear > > > > > > > for each definition. > > > > > > > > I still can't work out how to set contact photos reliably. I > > > thought I > > > > > > > had a fix yesterday, but it fails in some cases. Tiem for more > > > > > > > experiments... > > > > > > > > Richard > > > > > > > > On Nov 5, 2:11 am, "jak." <koda...@gmail.com> wrote: > > > > > > > > Thank you Dmitri, > > > > > > > > > Your response was very helpful. Along with that, and the sample > > > you > > > > > > > > posted on another thread about using both Contact Apis from one > > > app, > > > > > > > > I've gotten most of the way there. It is much appreciated. > > > > > > > > > I'm still however having some problems that I'm hard pressed to > > > find > > > > > a > > > > > > > > solution for. > > > > > > > > I'd be grateful if anyone could help me. > > > > > > > > > The biggest challenge I'm having with this API is that it's > > > > > > > > hard > > > for > > > > > > > > me to picture how the tables are laid out so I know which URI to > > > > > query > > > > > > > > to get the parts of the contact that I'm interested in. > > > > > > > > I found to get the email address for a contact I'm looking at I > > > can > > > > > > > > query the > > > > > > > > uri:ContactsContract.CommonDataKinds.Email.CONTENT_URI, > > > > > > > > looking at rows of the contact id i'm interested in. > > > > > > > > > However to get the note from the same contact I can't use a > > > similar > > > > > > > > pattern, because there is no > > > > > > > > ContactsContract.CommonDataKinds.Note.CONTENT_URI > > > > > > > > The Note type exists in CommonDataKinds but it doesn't have an > > > > > > > > associated CONTENT_URI. > > > > > > > > > I'm finding it very frustrating to use this API because every > > > time I > > > > > > > > go to try to pull out another piece of data from the contact, > > > > > > > > the > > > > > > > > method to access it seems to change (as I can't find a > > > > > > > > consistent > > > way > > > > > > > > to get a given field from a contact). And the documentation > > > > > > > > never > > > > > > > > describes how these keys, tables, and URIs are related. > > > > > > > > Basically > > > the > > > > > > > > ContactsContract documentation just gives you a giant list of > > ... > > read more » -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en