diff options
author | Jay Shrauner <shrauner@google.com> | 2013-08-08 10:24:43 -0700 |
---|---|---|
committer | Scott Kennedy <skennedy@google.com> | 2013-08-09 20:54:59 +0000 |
commit | 30d26326568cb888430468370df6f6ab2ec12767 (patch) | |
tree | fba6f5756ea5f8c8bf3e4b341ccfdee7016a4ed7 | |
parent | bc742af5bb568ed39dc37764587120928d068330 (diff) | |
download | android_frameworks_ex-30d26326568cb888430468370df6f6ab2ec12767.tar.gz android_frameworks_ex-30d26326568cb888430468370df6f6ab2ec12767.tar.bz2 android_frameworks_ex-30d26326568cb888430468370df6f6ab2ec12767.zip |
Add directory photo support
Load photos for directory thumbnail URIs by streaming directly from the URI.
Bug:7882158
Change-Id: Ifdf354c392b9aaff7c8fd6b73dc9bfe891825329
(cherry picked from commit 53d11c53b83bffe7607cffde994f505e775bcdbe)
-rw-r--r-- | chips/src/com/android/ex/chips/BaseRecipientAdapter.java | 60 |
1 files changed, 46 insertions, 14 deletions
diff --git a/chips/src/com/android/ex/chips/BaseRecipientAdapter.java b/chips/src/com/android/ex/chips/BaseRecipientAdapter.java index 0230c86..28127cc 100644 --- a/chips/src/com/android/ex/chips/BaseRecipientAdapter.java +++ b/chips/src/com/android/ex/chips/BaseRecipientAdapter.java @@ -46,7 +46,11 @@ import android.widget.Filterable; import android.widget.ImageView; import android.widget.TextView; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -750,34 +754,62 @@ public abstract class BaseRecipientAdapter extends BaseAdapter implements Filter } } + // For reading photos for directory contacts, this is the chunksize for + // copying from the inputstream to the output stream. + private static final int BUFFER_SIZE = 1024*16; + private void fetchPhotoAsync(final RecipientEntry entry, final Uri photoThumbnailUri) { - final AsyncTask<Void, Void, Void> photoLoadTask = new AsyncTask<Void, Void, Void>() { + final AsyncTask<Void, Void, byte[]> photoLoadTask = new AsyncTask<Void, Void, byte[]>() { @Override - protected Void doInBackground(Void... params) { + protected byte[] doInBackground(Void... params) { + // First try running a query. Images for local contacts are + // loaded by sending a query to the ContactsProvider. final Cursor photoCursor = mContentResolver.query( photoThumbnailUri, PhotoQuery.PROJECTION, null, null, null); if (photoCursor != null) { try { if (photoCursor.moveToFirst()) { - final byte[] photoBytes = photoCursor.getBlob(PhotoQuery.PHOTO); - entry.setPhotoBytes(photoBytes); - - mHandler.post(new Runnable() { - @Override - public void run() { - if (photoBytes != null) { - mPhotoCacheMap.put(photoThumbnailUri, photoBytes); - notifyDataSetChanged(); - } - } - }); + return photoCursor.getBlob(PhotoQuery.PHOTO); } } finally { photoCursor.close(); } + } else { + // If the query fails, try streaming the URI directly. + // For remote directory images, this URI resolves to the + // directory provider and the images are loaded by sending + // an openFile call to the provider. + try { + InputStream is = mContentResolver.openInputStream( + photoThumbnailUri); + if (is != null) { + byte[] buffer = new byte[BUFFER_SIZE]; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + int size; + while ((size = is.read(buffer)) != -1) { + baos.write(buffer, 0, size); + } + } finally { + is.close(); + } + return baos.toByteArray(); + } + } catch (IOException ex) { + // ignore + } } return null; } + + @Override + protected void onPostExecute(final byte[] photoBytes) { + entry.setPhotoBytes(photoBytes); + if (photoBytes != null) { + mPhotoCacheMap.put(photoThumbnailUri, photoBytes); + notifyDataSetChanged(); + } + } }; photoLoadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); } |