Android Bitmap Load loadBitmap(String url)

Here you can find the source of loadBitmap(String url)

Description

Load bitmap.

Parameter

Parameter Description
url the url

Return

the bitmap

Declaration

public static Bitmap loadBitmap(String url) 

Method Source Code

//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_);
    }
}

Related

  1. loadBitmap(Context c, String fileName)
  2. loadBitmap(Context c, String fileName, int width)
  3. loadBitmap(Context c, String fileName, int width, boolean sample)
  4. loadBitmap(InputStream is)
  5. loadBitmap(String bitmapUrl, ImageView imageView, int reqWidth, int reqHeight)
  6. loadBitmap(String url)
  7. loadBitmap(String url)
  8. loadBitmapAsset(Context context, String asset)
  9. loadBitmapFromAssets(Context context, String name)