Android Bitmap Load getBitmapFromUrl(String url)

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

Description

get Bitmap From Url

Declaration

public static Bitmap getBitmapFromUrl(String url) throws Exception 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap getBitmapFromUrl(String url) throws Exception {
        byte[] bytes = getBytesFromUrl(url);
        return byteToBitmap(bytes);
    }/* ww w  .j a va2s  .co  m*/

    public static byte[] getBytesFromUrl(String url) throws Exception {
        return readInputStream(getRequest(url));
    }

    public static Bitmap byteToBitmap(byte[] byteArray) {
        if (byteArray.length != 0) {
            return BitmapFactory.decodeByteArray(byteArray, 0,
                    byteArray.length);
        } else {
            return null;
        }
    }

    public static byte[] readInputStream(InputStream inStream)
            throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }

    public static InputStream getRequest(String path) throws Exception {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        if (conn.getResponseCode() == 200) {
            return conn.getInputStream();
        }
        return null;
    }
}

Related

  1. getBitmap(Context context, int resId)
  2. getBitmap(String photoId)
  3. getBitmapFromAsset(String strName, Context context)
  4. getBitmapFromInternet(String strName)
  5. getBitmapFromUri(Context ctxt, Uri selectedImageURI)
  6. loadBitmapFromFile(String fileNameWithPath)
  7. downloadBitmap(URI uri)
  8. returnBitMap(String path)
  9. getBitmapStoragePath(Context context)