List of usage examples for android.graphics BitmapFactory decodeStream
public static Bitmap decodeStream(InputStream is)
From source file:com.wrapp.android.webimage.ImageDownloader.java
public static boolean loadImage(final String imageKey, final URL imageUrl) { HttpEntity responseEntity = null;//from w w w. j a v a 2 s . com InputStream contentInputStream = null; try { final String imageUrlString = imageUrl.toString(); if (imageUrlString == null || imageUrlString.length() == 0) { throw new Exception("Passed empty URL"); } LogWrapper.logMessage("Requesting image '" + imageUrlString + "'"); final HttpClient httpClient = new DefaultHttpClient(); final HttpParams httpParams = httpClient.getParams(); httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, CONNECTION_TIMEOUT_IN_MS); httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT_IN_MS); httpParams.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, DEFAULT_BUFFER_SIZE); final HttpGet httpGet = new HttpGet(imageUrlString); final HttpResponse response = httpClient.execute(httpGet); responseEntity = response.getEntity(); if (responseEntity == null) { throw new Exception("No response entity for image: " + imageUrl.toString()); } contentInputStream = responseEntity.getContent(); if (contentInputStream == null) { throw new Exception("No content stream for image: " + imageUrl.toString()); } Bitmap bitmap = BitmapFactory.decodeStream(contentInputStream); ImageCache.saveImageInFileCache(imageKey, bitmap); LogWrapper.logMessage("Downloaded image: " + imageUrl.toString()); bitmap.recycle(); } catch (IOException e) { LogWrapper.logException(e); return false; } catch (Exception e) { LogWrapper.logException(e); return false; } finally { try { if (contentInputStream != null) { contentInputStream.close(); } if (responseEntity != null) { responseEntity.consumeContent(); } } catch (IOException e) { LogWrapper.logException(e); } } return true; }
From source file:me.nytyr.simplebitmapcache.http.HttpBitmapGetter.java
@Override public Bitmap get(final String URL) { // Making HTTP request try {/*from w ww . j av a2s .c o m*/ final HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, httpTimeout); HttpConnectionParams.setSoTimeout(httpParams, httpTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); ConnManagerParams.setTimeout(httpParams, httpTimeout); DefaultHttpClient httpClient = new DefaultHttpClient(httpParams); HttpGet httpPost = new HttpGet(URL); HttpResponse httpResponse = httpClient.execute(httpPost); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); HttpEntity httpEntity = httpResponse.getEntity(); final FlushedInputStream is = new FlushedInputStream(httpEntity.getContent()); if (statusCode != 200) { Log.e(TAG, "Error downloading image. Status code > " + statusCode); return null; } return BitmapFactory.decodeStream(is); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:net.zorgblub.typhon.catalog.LoadThumbnailTask.java
@Override public Option<FastBitmapDrawable> doInBackground(Link... entries) { this.imageLink = entries[0]; if (imageLink != null) { String href = imageLink.getHref(); try {//from w ww .ja v a 2s. c o m String target = new URL(new URL(baseUrl), href).toString(); Log.i("LoadThumbnailTask", "Downloading image: " + target); HttpGet currentRequest = new HttpGet(target); HttpResponse resp = httpClient.execute(currentRequest); Bitmap bitmap = BitmapFactory.decodeStream(resp.getEntity().getContent()); return some(new FastBitmapDrawable(bitmap)); } catch (Exception | OutOfMemoryError e) { //Ignore and exit. } } return none(); }
From source file:com.aujur.ebookreader.catalog.LoadThumbnailTask.java
@Override protected Void doInBackground(Link... entries) { this.imageLink = entries[0]; if (imageLink == null) { return null; }//w w w. j av a2 s.co m String href = imageLink.getHref(); try { String target = new URL(new URL(baseUrl), href).toString(); Log.i("LoadThumbnailTask", "Downloading image: " + target); HttpGet currentRequest = new HttpGet(target); HttpResponse resp = httpClient.execute(currentRequest); Bitmap bitmap = BitmapFactory.decodeStream(resp.getEntity().getContent()); this.drawable = new FastBitmapDrawable(bitmap); } catch (Exception io) { //Ignore and exit. } catch (OutOfMemoryError error) { } return null; }
From source file:mobisocial.musubi.util.OGUtil.java
public static OGData getOrGuess(String url) { DefaultHttpClient hc = new DefaultHttpClient(); HttpResponse res;/* ww w. j a va 2 s. co m*/ try { HttpGet hg = new HttpGet(url); res = hc.execute(hg); } catch (Exception e) { Log.e(TAG, "unable to fetch page to get og tags", e); return null; } String location = url; //TODO: if some kind of redirect magic happened, then //make the location match that OGData og = new OGData(); HttpEntity he = res.getEntity(); Header content_type = he.getContentType(); //TODO: check the content directly if they forget the type header if (content_type == null || content_type.getValue() == null) { Log.e(TAG, "page missing content type ..abandoning: " + url); return null; } og.mMimeType = content_type.getValue(); //just make a thumbnail if the shared item is an image if (og.mMimeType.startsWith("image/")) { Bitmap b; try { b = BitmapFactory.decodeStream(he.getContent()); } catch (Exception e) { return null; } //TODO: scaling int w = b.getWidth(); int h = b.getHeight(); if (w > h) { h = h * 200 / w; w = 200; } else { w = w * 200 / h; h = 200; } Bitmap b2 = Bitmap.createScaledBitmap(b, w, h, true); b.recycle(); b = b2; ByteArrayOutputStream baos = new ByteArrayOutputStream(); b.compress(CompressFormat.PNG, 100, baos); og.mImage = baos.toByteArray(); b.recycle(); return og; } //if its not html, we can't extract more details, the caller //should rely on what they already know. if (!og.mMimeType.startsWith("text/html") && !og.mMimeType.startsWith("application/xhtml")) { Log.e(TAG, "shared content is not a known type for meta data processing " + og.mMimeType); return og; } String html; try { html = IOUtils.toString(he.getContent()); } catch (Exception e) { Log.e(TAG, "failed to read html content", e); return og; } Matcher m = sTitleRegex.matcher(html); if (m.find()) { og.mTitle = StringEscapeUtils.unescapeHtml4(m.group(1)); } m = sMetaRegex.matcher(html); int offset = 0; String raw_description = null; while (m.find(offset)) { try { String meta_tag = m.group(); Matcher mp = sPropertyOfMeta.matcher(meta_tag); if (!mp.find()) continue; String type = mp.group(1); type = type.substring(1, type.length() - 1); Matcher md = sContentOfMeta.matcher(meta_tag); if (!md.find()) continue; String data = md.group(1); //remove quotes data = data.substring(1, data.length() - 1); data = StringEscapeUtils.unescapeHtml4(data); if (type.equalsIgnoreCase("og:title")) { og.mTitle = data; } else if (type.equalsIgnoreCase("og:image")) { HttpResponse resi; try { HttpGet hgi = new HttpGet(data); resi = hc.execute(hgi); } catch (Exception e) { Log.e(TAG, "unable to fetch og image url", e); continue; } HttpEntity hei = resi.getEntity(); if (!hei.getContentType().getValue().startsWith("image/")) { Log.e(TAG, "image og tag points to non image data" + hei.getContentType().getValue()); } try { Bitmap b; try { b = BitmapFactory.decodeStream(hei.getContent()); } catch (Exception e) { return null; } //TODO: scaling int w = b.getWidth(); int h = b.getHeight(); if (w > h) { h = h * Math.min(200, w) / w; w = Math.min(200, w); } else { w = w * Math.min(200, h) / h; h = Math.min(200, h); } Bitmap b2 = Bitmap.createScaledBitmap(b, w, h, true); b.recycle(); b = b2; ByteArrayOutputStream baos = new ByteArrayOutputStream(); b.compress(CompressFormat.PNG, 100, baos); b.recycle(); og.mImage = baos.toByteArray(); } catch (Exception e) { Log.e(TAG, "failed to fetch image for og", e); continue; } } else if (type.equalsIgnoreCase("description")) { raw_description = data; } else if (type.equalsIgnoreCase("og:description")) { og.mDescription = data; } else if (type.equalsIgnoreCase("og:url")) { og.mUrl = data; } } finally { offset = m.end(); } } HashSet<String> already_fetched = new HashSet<String>(); if (og.mImage == null) { int max_area = 0; m = sImageRegex.matcher(html); int img_offset = 0; while (m.find(img_offset)) { try { String img_tag = m.group(); Matcher ms = sSrcOfImage.matcher(img_tag); if (!ms.find()) continue; String img_src = ms.group(1); img_src = img_src.substring(1, img_src.length() - 1); img_src = StringEscapeUtils.unescapeHtml4(img_src); //don't fetch an image twice (like little 1x1 images) if (already_fetched.contains(img_src)) continue; already_fetched.add(img_src); HttpResponse resi; try { HttpGet hgi = new HttpGet(new URL(new URL(location), img_src).toString()); resi = hc.execute(hgi); } catch (Exception e) { Log.e(TAG, "unable to fetch image url for biggest image search" + img_src, e); continue; } HttpEntity hei = resi.getEntity(); if (hei == null) { Log.w(TAG, "image missing en ..trying entity response: " + url); continue; } Header content_type_image = hei.getContentType(); if (content_type_image == null || content_type_image.getValue() == null) { Log.w(TAG, "image missing content type ..trying anyway: " + url); } if (!content_type_image.getValue().startsWith("image/")) { Log.w(TAG, "image tag points to non image data " + hei.getContentType().getValue() + " " + img_src); } try { Bitmap b; try { b = BitmapFactory.decodeStream(hei.getContent()); } catch (Exception e) { return null; } //TODO: scaling int w = b.getWidth(); int h = b.getHeight(); if (w * h <= max_area) { continue; } if (w < 32 || h < 32) { //skip dinky crap continue; } if (w > h) { h = h * Math.min(200, w) / w; w = Math.min(200, w); } else { w = w * Math.min(200, h) / h; h = Math.min(200, h); } Bitmap b2 = Bitmap.createScaledBitmap(b, w, h, true); b.recycle(); b = b2; ByteArrayOutputStream baos = new ByteArrayOutputStream(); b.compress(CompressFormat.PNG, 100, baos); og.mImage = baos.toByteArray(); b.recycle(); max_area = w * h; } catch (Exception e) { Log.e(TAG, "failed to fetch image for og", e); continue; } } finally { img_offset = m.end(); } } } if (og.mDescription == null) og.mDescription = raw_description; return og; }
From source file:com.squeezecontrol.image.HttpFetchingImageStore.java
@Override public Bitmap getImage(String url) { HttpResponse response = null;// w ww . j a v a 2s .c o m try { HttpGet get = new HttpGet(baseUrl == null ? url : baseUrl + url); response = (BasicHttpResponse) mClient.execute(get); return BitmapFactory.decodeStream(response.getEntity().getContent()); } catch (IOException e) { return null; } finally { if (response != null) try { response.getEntity().consumeContent(); } catch (IOException e) { } } }
From source file:ch.fixme.status.Net.java
public Bitmap getBitmap() { try {//from w w w . j ava 2 s .co m HttpResponse response = client.execute(getMethod); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { final HttpEntity entity = response.getEntity(); if (entity != null) { InputStream is = null; try { is = entity.getContent(); return BitmapFactory.decodeStream(new FlushedInputStream(is)); } finally { if (is != null) { is.close(); } entity.consumeContent(); } } } } catch (Throwable t) { Log.e(Main.TAG, "Exception fetching data", t); } return null; }
From source file:com.fenlisproject.elf.core.net.HttpRequest.java
public Bitmap getBitmapContent() { return BitmapFactory.decodeStream(getInputStream()); }
From source file:forplay.android.AndroidAssetManager.java
Bitmap doGetBitmap(String path) { try {/*ww w.ja va 2 s.c om*/ InputStream is = openResource(path); if (is == null) { // TODO: This should return an error image like JavaAssetManager does throw new RuntimeException("Unable to load image " + path); } try { return BitmapFactory.decodeStream(is); } finally { is.close(); } } catch (IOException e) { // TODO: This should return an error image like JavaAssetManager does throw new RuntimeException(e); } }
From source file:org.uoyabause.android.AsyncDownload.java
private Bitmap downloadImage(String uri) { Log.d("downloadImage", "uri=" + uri); try {//ww w .ja v a 2 s . co m hGetMethod.setURI(new URI(uri)); HttpResponse resp = hClient.execute(hGetMethod); if (resp.getStatusLine().getStatusCode() < 400) { Log.d("downloadImage", "try"); InputStream is = resp.getEntity().getContent(); Bitmap bit = BitmapFactory.decodeStream(is); return bit; } } catch (Exception e) { Log.d("downloadImage", "error"); e.printStackTrace(); } return null; }