Back to project page PhotoGallery.
The source code is released under:
MIT License
If you think the Android project PhotoGallery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.ambergleam.photogallery; //w w w . j a v a 2 s .c om import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.HandlerThread; import android.os.Message; import android.util.Log; public class ThumbnailDownloader<Token> extends HandlerThread { private static final String TAG = "TumbnailDownloader"; private static final int MESSAGE_DOWNLOAD = 0; Handler mHandler; Map<Token, String> requestMap = Collections.synchronizedMap(new HashMap<Token, String>()); Handler mResponseHandler; Listener<Token> mListener; public interface Listener<Token> { void onThumbnailDownloaded(Token token, Bitmap thumbnail); } public void setListener(Listener<Token> listener) { mListener = listener; } @SuppressLint("HandlerLeak") @Override protected void onLooperPrepared() { mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == MESSAGE_DOWNLOAD) { @SuppressWarnings("unchecked") Token token = (Token) msg.obj; Log.i(TAG, "Got a request for url: " + requestMap.get(token)); handleRequest(token); } } }; } public ThumbnailDownloader(Handler responseHandler) { super(TAG); mResponseHandler = responseHandler; } public void queueThumbnail(Token token, String url) { Log.i(TAG, "Got an URL: " + url); requestMap.put(token, url); mHandler.obtainMessage(MESSAGE_DOWNLOAD, token).sendToTarget(); } private void handleRequest(final Token token) { try { final String url = requestMap.get(token); if (url == null) { return; } byte[] bitmapBytes = new FlickrFetchr().getUrlBytes(url); final Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length); Log.i(TAG, "Bitmap created"); mResponseHandler.post(new Runnable() { @Override public void run() { if (requestMap.get(token) != url) { return; } requestMap.remove(token); mListener.onThumbnailDownloaded(token, bitmap); } }); } catch (IOException ioe) { Log.e(TAG, "Error downloading image", ioe); } } public void clearQueue() { mHandler.removeMessages(MESSAGE_DOWNLOAD); requestMap.clear(); } }