Here you can find the source of loadBitmap(String url)
Parameter | Description |
---|---|
url | the url |
public static Bitmap loadBitmap(String url)
//package com.java2s; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Main { /** The Constant IO_BUFFER_SIZE. */ private static final int IO_BUFFER_SIZE = 1024; /**//from w ww . j a va2s . com * Load bitmap. * * @param url the url * @return the bitmap */ public static Bitmap loadBitmap(String url) { Bitmap bitmap = null; InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE); copy(in, out); out.flush(); final byte[] data = dataStream.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); } catch (IOException e) { // Log.e(TAG, "Could not load Bitmap from: " + url); } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } return bitmap; } /** * Copy. * * @param in the in * @param out the out * @throws IOException Signals that an I/O exception has occurred. */ private static void copy(InputStream in, BufferedOutputStream out) throws IOException { int byte_; while ((byte_ = in.read()) != -1) out.write(byte_); } }