From 163f29750a21e734fb5ae5b2894d8660b198b688 Mon Sep 17 00:00:00 2001 From: ermau Date: Tue, 10 Sep 2013 15:07:09 -0400 Subject: [PATCH] [AD] Avoid async query to get thumbnail data This fixes #12 by just executing the query synchronously on our own thread instead of using an async query which requires a looper. --- MonoDroid/Xamarin.Mobile/Contacts/Contact.cs | 58 +++++++++----------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/MonoDroid/Xamarin.Mobile/Contacts/Contact.cs b/MonoDroid/Xamarin.Mobile/Contacts/Contact.cs index 1602062..4e2985a 100644 --- a/MonoDroid/Xamarin.Mobile/Contacts/Contact.cs +++ b/MonoDroid/Xamarin.Mobile/Contacts/Contact.cs @@ -17,10 +17,12 @@ using System; using System.Collections.Generic; using System.IO; +using System.Threading; using System.Threading.Tasks; using Android.Content; using Android.Database; using Android.Graphics; +using Android.OS; using Android.Provider; using Xamarin.Media; @@ -150,26 +152,43 @@ public IEnumerable Phones } public Bitmap GetThumbnail() + { + byte[] data = GetThumbnailBytes(); + return (data == null) ? null : BitmapFactory.DecodeByteArray (data, 0, data.Length); + } + + public Task SaveThumbnailAsync (string path) + { + if (path == null) + throw new ArgumentNullException ("path"); + + return Task.Factory.StartNew (() => { + byte[] bytes = GetThumbnailBytes(); + if (bytes == null) + return null; + + File.WriteAllBytes (path, bytes); + return new MediaFile (path, deletePathOnDispose: false); + }); + } + + byte[] GetThumbnailBytes() { string lookupColumn = (IsAggregate) ? ContactsContract.ContactsColumns.LookupKey : ContactsContract.RawContactsColumns.ContactId; ICursor c = null; - try - { + try { c = this.content.Query (ContactsContract.Data.ContentUri, new[] { ContactsContract.CommonDataKinds.Photo.PhotoColumnId, ContactsContract.DataColumns.Mimetype }, lookupColumn + "=? AND " + ContactsContract.DataColumns.Mimetype + "=?", new[] { Id, ContactsContract.CommonDataKinds.Photo.ContentItemType }, null); - while (c.MoveToNext()) - { + while (c.MoveToNext()) { byte[] tdata = c.GetBlob (c.GetColumnIndex (ContactsContract.CommonDataKinds.Photo.PhotoColumnId)); if (tdata != null) - return BitmapFactory.DecodeByteArray (tdata, 0, tdata.Length); + return tdata; } - } - finally - { + } finally { if (c != null) c.Close(); } @@ -177,29 +196,6 @@ public Bitmap GetThumbnail() return null; } - public Task SaveThumbnailAsync (string path) - { - if (path == null) - throw new ArgumentNullException ("path"); - - string lookupColumn = (IsAggregate) - ? ContactsContract.ContactsColumns.LookupKey - : ContactsContract.RawContactsColumns.ContactId; - - AsyncQuery query = new AsyncQuery (this.content, c => c.GetBlob (c.GetColumnIndex (ContactsContract.CommonDataKinds.Photo.PhotoColumnId))); - query.StartQuery (0, null, ContactsContract.Data.ContentUri, new[] { ContactsContract.CommonDataKinds.Photo.PhotoColumnId, ContactsContract.DataColumns.Mimetype }, - lookupColumn + "=? AND " + ContactsContract.DataColumns.Mimetype + "=?", new[] { Id, ContactsContract.CommonDataKinds.Photo.ContentItemType }, null); - - return query.Task.ContinueWith (t => - { - if (t.Result == null) - return null; - - File.WriteAllBytes (path, t.Result); - return new MediaFile (path, deletePathOnDispose: false); - }, TaskScheduler.Default); - } - private readonly ContentResolver content; } } \ No newline at end of file