Android Open Source - photos Image Downloader






From Project

Back to project page photos.

License

The source code is released under:

MIT License

If you think the Android project photos listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ericfarraro.photos.core;
/*from ww  w.java  2  s  . c om*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.widget.ImageView;

import com.ericfarraro.sdk.interfaces.ImageDownloadCompleted;
import com.ericfarraro.sdk.util.Utility;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Eric on 10/7/2014.
 */
public class ImageDownloader extends HandlerThread {

    public static final String NAME = "ImageDownloader";
    public static final int MESSAGE_DOWNLOAD_IMAGE = 0;

    Handler mHandler;
    final Map<ImageView, String> mRequestMap =
            Collections.synchronizedMap(new HashMap<ImageView, String>());
    protected ImageDownloadCompleted mListener;

    public ImageDownloader() {
        super(NAME);
    }

    /**
     * Requests the the image URL be added to the download queue
     * @param token A token uniquely identifying the image
     * @param url The URL of the image
     */
    public void queueImageUrl(ImageView token, String url) {
        mRequestMap.put(token, url);
        mHandler.obtainMessage(MESSAGE_DOWNLOAD_IMAGE, token).sendToTarget();
    }

    @Override
    protected void onLooperPrepared() {
        super.onLooperPrepared();

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == MESSAGE_DOWNLOAD_IMAGE) {
                    handleRequest((ImageView)msg.obj);
                }
            }
        };
    }

    protected void handleRequest(final ImageView imageView) {

        final String url = mRequestMap.get(imageView);

        if(url == null)
            return;

        try {
            byte[] imageBytes = Utility.getBytesForUrl(url);
            final Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    // if the request for this ImageView (token) has changed, ignore it
                    if(mRequestMap.get(imageView) != url)
                        return;

                    mRequestMap.remove(imageView);

                    // notify listeners
                    if(mListener != null) {
                        mListener.onImageDownloadCompleted(imageView, bitmap);
                    }
                }
            });
        } catch(IOException e) {
            // some error occurred getting the bytes

            mHandler.post(new Runnable() {
                @Override
                public void run() {

                    mRequestMap.remove(imageView);

                    // notify listeners
                    if(mListener != null) {
                        mListener.onImageDownloadCompleted(imageView, null);
                    }
                }
            });

        }
    }

    public ImageDownloadCompleted getListener() {
        return mListener;
    }

    public void setListener(ImageDownloadCompleted listener) {
        mListener = listener;
    }
}




Java Source Code List

com.ericfarraro.photos.ApplicationTest.java
com.ericfarraro.photos.activities.MainActivity.java
com.ericfarraro.photos.activities.PhotoDetailActivity.java
com.ericfarraro.photos.adapters.PhotoGalleryItemListAdapter.java
com.ericfarraro.photos.core.EndlessScrollListener.java
com.ericfarraro.photos.core.ImageDownloader.java
com.ericfarraro.photos.fragments.MainFragment.java
com.ericfarraro.photos.fragments.PhotoDetailFragment.java
com.ericfarraro.photos.singletons.PhotoCache.java
com.ericfarraro.sdk.core.ImageDownloader.java
com.ericfarraro.sdk.data.FlickrPhotoSource.java
com.ericfarraro.sdk.data.PhotoSource.java
com.ericfarraro.sdk.interfaces.ImageDownloadCompleted.java
com.ericfarraro.sdk.interfaces.PhotoListRequestCompleted.java
com.ericfarraro.sdk.interfaces.UrlContentRetrieved.java
com.ericfarraro.sdk.models.Photo.java
com.ericfarraro.sdk.util.UrlFetchTask.java
com.ericfarraro.sdk.util.Utility.java