get Drawable From Url : Drawable « 2D Graphics « Android






get Drawable From Url

  
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

class IOUtils {

  private static final String LOG_TAG = "IOUtils";
  public static final String PREFS_FILE = "javaeye.prefs";

  public static Drawable getDrawableFromUrl(URL url) {
    try {
      InputStream is = url.openStream();
      Drawable d = Drawable.createFromStream(is, "src");
      return d;
    } catch (MalformedURLException e) {
      // e.printStackTrace();
    } catch (IOException e) {
      // e.printStackTrace();
    }
    return null;
  }

  private static void copy(InputStream in, OutputStream out)
      throws IOException {
    byte[] b = new byte[4 * 1024];
    int read;
    while ((read = in.read(b)) != -1) {
      out.write(b, 0, read);
    }
  }

  private static void closeStream(Closeable stream) {
    if (stream != null) {
      try {
        stream.close();
      } catch (IOException e) {
        // Log.e(LOG_TAG, e.getMessage());
      }
    }
  }

}

   
    
  








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.createFromStream
6.Drawable Manager
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