Drawable.createFromStream : Drawable « 2D Graphics « Android


//package com.myspace.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

class MSDrawableManager {
    private final Map<String, Drawable> drawableMap;
    private ExecutorService executorService;

    static MSDrawableManager instance;
    static public MSDrawableManager getInstance() {
      if (instance == null) {
        synchronized(MSDrawableManager.class) {
          if (instance == null) {
            instance = new MSDrawableManager(); 
      return instance;
    private MSDrawableManager() {
      drawableMap = new HashMap<String, Drawable>();
      executorService = Executors.newFixedThreadPool(2);
    public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
      if (drawableMap.containsKey(urlString)) {

      final Handler handler = new Handler() {
        public void handleMessage(Message message) {
          imageView.setImageDrawable((Drawable) message.obj);

      executorService.execute(new Runnable() {
        public void run() {
          Drawable drawable = fetchDrawable(urlString);
          Message message = handler.obtainMessage(1, drawable);
    public Drawable fetchDrawable(String urlString) {
      if (drawableMap.containsKey(urlString)) {
        return drawableMap.get(urlString);

      try {
        InputStream is = fetch(urlString);
        Drawable drawable = Drawable.createFromStream(is, "src");
        drawableMap.put(urlString, drawable);
        return drawable;
      } catch (Exception e) {
        return null; 

    private InputStream fetch(String urlString) throws MalformedURLException, IOException {
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpGet request = new HttpGet(urlString);
      HttpResponse response = httpClient.execute(request);
      return response.getEntity().getContent();


Related examples in the same category

1.Animate Drawables
2.ShapeDrawable Demo
3.Using GradientDrawable
4.Gets the image drawable from the url
5.Drawable Manager
6.get Drawable From Url
7.simply resizes a given drawable resource to the given width and height
8.Drawable.createFromStream and URL connection
9.Drawable Manager with Thread
10.get Drawable From Web Operation
11.Center a Drawable
12.Rotate around Center Point