Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This is an object that can load images from a Uri on a thread.
- *
- * @author Jeremy Wadsack
- */
- public class ImageThreadLoader {
- public static final String LOG_TAG = ImageThreadLoader.class.getCanonicalName();
- // Global cache of images.
- // Using SoftReference to allow garbage collector to clean cache if needed
- private final HashMap<String, SoftReference<Drawable>> Cache = new HashMap<String, SoftReference<Drawable>>();
- private final class QueueItem {
- public Uri uri;
- public ImageLoadedListener listener;
- }
- private final ArrayList<QueueItem> Queue = new ArrayList<QueueItem>();
- private final Handler handler = new Handler();
- private Thread thread;
- private Context context;
- private QueueRunner runner = new QueueRunner(); ;
- /** Creates a new instance of the ImageThreadLoader */
- public ImageThreadLoader ( Context context ) {
- thread = new Thread( runner );
- this.context = context;
- }
- /**
- * Defines an interface for a callback that will handle responses from the
- * thread loader when an image is done being loaded.
- */
- public interface ImageLoadedListener {
- public void imageLoaded ( Drawable imageDrawable );
- }
- /**
- * Provides a Runnable class to handle loading the image from the URL and
- * settings the ImageView on the UI thread.
- */
- private class QueueRunner implements Runnable {
- public void run () {
- synchronized ( this ) {
- while ( Queue.size() > 0 ) {
- final QueueItem item = Queue.remove( 0 );
- // If in the cache, return that copy and be done
- if ( Cache.containsKey( item.uri ) && Cache.get( item.uri ) != null ) {
- // Use a handler to get back onto the UI thread for the
- // update
- handler.post( new Runnable() {
- public void run () {
- if ( item.listener != null ) {
- SoftReference<Drawable> ref = Cache.get( item.uri.toString() );
- if ( ref != null ) {
- item.listener.imageLoaded( ref.get() );
- }
- }
- }
- } );
- }
- else {
- final Drawable img = readBitmapFromUri( item.uri );
- if ( img != null ) {
- Cache.put( item.uri.toString(), new SoftReference<Drawable>( img ) );
- // Use a handler to get back onto the UI thread for
- // the update
- handler.post( new Runnable() {
- public void run () {
- if ( item.listener != null ) {
- item.listener.imageLoaded( img );
- }
- }
- } );
- }
- }
- }
- }
- }
- }
- /**
- * Queues up a uri to load an image from for a given image view.
- *
- * @param uri
- * The URI source of the image
- * @param callback
- * The listener class to call when the image is loaded
- * @throws MalformedURLException
- * If the provided uri cannot be parsed
- * @return A Bitmap image if the image is in the cache, else null.
- */
- public Drawable loadImage ( final String uri, final ImageLoadedListener listener ) {
- // If it's in the cache, just get it and quit it
- if ( Cache.containsKey( uri ) ) {
- SoftReference<Drawable> ref = Cache.get( uri );
- if ( ref != null ) {
- return ref.get();
- }
- }
- QueueItem item = new QueueItem();
- item.uri = Uri.parse( uri );
- item.listener = listener;
- Queue.add( item );
- // start the thread if needed
- if ( thread.getState() == State.NEW ) {
- thread.start();
- }
- else if ( thread.getState() == State.TERMINATED ) {
- thread = new Thread( runner );
- thread.start();
- }
- return null;
- }
- public Drawable readBitmapFromUri ( Uri uri ) {
- try {
- return Drawable.createFromStream( context.getContentResolver().openInputStream( uri ), null );
- }
- catch ( Exception e ) {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment