Drawable.createFromStream : Drawable « 2D Graphics « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » 2D Graphics » Drawable 
Drawable.createFromStream
  
//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)) {
        imageView.setImageDrawable(drawableMap.get(urlString));
      }
      
      imageView.setImageDrawable(null);

      final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
          imageView.setImageDrawable((Drawablemessage.obj);
        }
      };

      executorService.execute(new Runnable() {
        public void run() {
          Drawable drawable = fetchDrawable(urlString);
          Message message = handler.obtainMessage(1, drawable);
          handler.sendMessage(message);
        }
      });
    }
    
    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 urlStringthrows 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.