Android Drawable Create loadDrawable(final String imageUrl, final ImageView imageView, final ImageCallback imageCallback)

Here you can find the source of loadDrawable(final String imageUrl, final ImageView imageView, final ImageCallback imageCallback)

Description

load Drawable

Declaration

public static Drawable loadDrawable(final String imageUrl,
            final ImageView imageView, final ImageCallback imageCallback) 

Method Source Code

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;

public class Main{
    private static final String TAG = "AsyncImageLoader";
    public static String CACHE_PATH;
    private static HashMap<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
    public static Drawable loadDrawable(final String imageUrl,
            final ImageView imageView, final ImageCallback imageCallback) {
        Log.d(TAG, "loadDrawable(), url = " + imageUrl);
        if (imageCache.containsKey(imageUrl)) {
            // get from cache
            SoftReference<Drawable> softReference = imageCache
                    .get(imageUrl);//from  w w  w .java  2 s .  co  m
            Drawable drawable = softReference.get();
            if (drawable != null) {
                Log.w(TAG, "loadDrawable(), find url incache: " + imageUrl);
                return drawable;
            }
        }

        final Handler handler = new Handler() {
            public void handleMessage(Message message) {
                imageCallback.imageLoaded((Drawable) message.obj,
                        imageView, imageUrl);
            }
        };

        // create a new thread to get image
        new Thread() {
            @Override
            public void run() {
                Drawable drawable = loadImageFromUrl(imageUrl);
                if (drawable != null) {
                    imageCache.put(imageUrl, new SoftReference<Drawable>(
                            drawable));
                    Message message = handler.obtainMessage(0, drawable);
                    handler.sendMessage(message);
                }
            }
        }.start();

        return null;
    }
    private static Drawable loadImageFromUrl(String url) {

        if (url == null || url.length() == 0) {
            return null;
        }

        try {
            return ImageHelper.generateImageDrawable(new URL(url), false);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    /**
     * Down load bitmap from net and save it to cache
     * 
     * @param urlString
     *            The address of bitmap
     * @param file
     *            Bitmap will been saved path
     * @return null indicator down load bitmap is fail
     */
    private Bitmap loadImageFromUrl(String urlString, boolean cache) {
        Bitmap bitmap = null;
        HttpURLConnection conn = null;
        InputStream is = null;
        File file = new File(CACHE_PATH + urlString);

        try {
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            is = conn.getInputStream();
            // Get the length
            int length = (int) conn.getContentLength();
            if (length != -1) {
                byte[] imgData = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, imgData, destPos, readLen);
                    destPos += readLen;
                }

                bitmap = BitmapFactory.decodeByteArray(imgData, 0,
                        imgData.length);

                // Save to cache
                if (file != null) {
                    // writeBitmapToCache(imgData, file);
                }
            }

            if (is != null) {
                is.close();
            }

            if (conn != null) {
                conn.disconnect();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }

        return bitmap;
    }
}

Related

  1. getDrawableById(Context paramContext, String paramString)
  2. getDrawableFromUrl(String url)
  3. getPressedDrawable(int color)
  4. getDrawableByName(Context ctx, String name)