vijay
6/3/2015 - 12:06 PM

getting bitmap from URL

getting bitmap from URL

public static Bitmap downloadBitmap(String urlStr) {
		long MAX_BM_SIZE = 1024 * 5;
		InputStream is = null;
		BufferedInputStream bis = null;
		Bitmap bmp = null;
		try {
			URL url = new URL(urlStr);
			URLConnection conn = url.openConnection();
			conn.connect();
			is = conn.getInputStream();
			bis = new BufferedInputStream(is);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte buf[] = new byte[1024];
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];

			int len = 0;
			while ((len = bis.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}

			buf = out.toByteArray();
			Log.e("AVAIL", buf.length + "--" + MAX_BM_SIZE);
			if (buf.length > MAX_BM_SIZE) {
				Options opt = new Options();
				opt.inScaled = true;
				opt.inDither = true;
				opt.inSampleSize = 2;
				opt.inTempStorage = new byte[16000];
				opt.inJustDecodeBounds = true;
				BitmapFactory.decodeByteArray(buf, 0, buf.length, opt);

				if (opt.outWidth > 480 && opt.outHeight > 800)
					opt.inSampleSize = 4;
				opt.inJustDecodeBounds = false;
				bmp = BitmapFactory.decodeByteArray(buf, 0, buf.length, opt);
			} else
				bmp = BitmapFactory.decodeByteArray(buf, 0, buf.length);

			out.close();
			if (is != null)
				is.close();
			if (bis != null)
				bis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bmp;
	}