List of usage examples for android.graphics BitmapFactory decodeStream
public static Bitmap decodeStream(InputStream is)
From source file:com.xianzhi.tool.web.img.ImageFetcher.java
/** * The main process method, which will be called by the ImageWorker in the AsyncTask background * thread.//from www . ja v a 2 s . co m * * @param data The data to load the bitmap, in this case, a regular http URL * @return The downloaded and resized bitmap */ private Bitmap processBitmap(String url) { Bitmap bitmap = null; // AndroidHttpClient is not allowed to be used from the main thread final HttpClient client = HttpsClient.getInstance().getHttpsClient(); if (HttpJsonTool.cookieInfo != null) { ((AbstractHttpClient) client).setCookieStore(HttpJsonTool.cookieInfo); } final HttpGet getRequest = new HttpGet(url); try { HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = entity.getContent(); try { bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream)); } catch (OutOfMemoryError e) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream), null, options); } finally { if (inputStream != null) { inputStream.close(); inputStream = null; } entity.consumeContent(); } } return bitmap; } catch (IOException e) { getRequest.abort(); Log.w("LOG_TAG", "I/O error while retrieving bitmap from " + url, e); } catch (IllegalStateException e) { getRequest.abort(); Log.w("LOG_TAG", "Incorrect URL: " + url); } catch (Exception e) { getRequest.abort(); Log.w("LOG_TAG", "Error while retrieving bitmap from " + url, e); } finally { if ((client instanceof AndroidHttpClient)) { ((AndroidHttpClient) client).close(); } } return null; }
From source file:com.adobe.plugins.FastCanvasRenderer.java
private void flushQueue() { synchronized (this) { if (!FastCanvas.copyMessageQueue(mLocalQueue)) { // Tear down. if any to be done. return; }//from w w w . ja va 2 s . c om } mRenderCommands = ""; FastCanvasMessage m; while (mLocalQueue.size() > 0) { m = mLocalQueue.remove(); if (m.type == FastCanvasMessage.Type.LOAD) { Activity theActivity = FastCanvas.getActivity(); if (theActivity != null) { // If we are re-using a texture ID, unload the old texture for (int i = 0; i < mTextures.size(); i++) { if (mTextures.get(i).id == m.textureID) { unloadTexture(m.textureID); break; } } // Load and track the texture String path = "www/" + m.url; boolean success = false; FastCanvasTextureDimension dim = new FastCanvasTextureDimension(); // See the following for why PNG files with premultiplied alpha and GLUtils don't get along // http://stackoverflow.com/questions/3921685/issues-with-glutils-teximage2d-and-alpha-in-textures if (path.toLowerCase(Locale.US).endsWith(".png")) { success = FastCanvasJNI.addPngTexture(theActivity.getAssets(), path, m.textureID, dim); if (success == false) { Log.i("CANVAS", "CanvasRenderer loadTexture failed to load PNG in native code, falling back to GLUtils."); } } if (success == false) { try { InputStream instream = theActivity.getAssets().open(path); final Bitmap bmp = BitmapFactory.decodeStream(instream); loadTexture(bmp, m.textureID); dim.width = bmp.getWidth(); dim.height = bmp.getHeight(); success = true; } catch (IOException e) { Log.i("CANVAS", "CanvasRenderer loadTexture error=", e); m.callbackContext.error(e.getMessage()); } } if (success == true) { FastCanvasTexture t = new FastCanvasTexture(m.url, m.textureID); mTextures.add(t); JSONArray args = new JSONArray(); args.put(dim.width); args.put(dim.height); m.callbackContext.success(args); } } } else if (m.type == FastCanvasMessage.Type.UNLOAD) { // Stop tracking the texture for (int i = 0; i < mTextures.size(); i++) { if (mTextures.get(i).id == m.textureID) { mTextures.remove(i); break; } } unloadTexture(m.textureID); } else if (m.type == FastCanvasMessage.Type.RELOAD) { Activity theActivity = FastCanvas.getActivity(); if (theActivity != null) { // Reload the texture String path = "www/" + m.url; boolean success = false; if (path.toLowerCase(Locale.US).endsWith(".png")) { success = FastCanvasJNI.addPngTexture(theActivity.getAssets(), path, m.textureID, null); } if (success == false) { try { InputStream instream = theActivity.getAssets().open(path); final Bitmap bmp = BitmapFactory.decodeStream(instream); loadTexture(bmp, m.textureID); } catch (IOException e) { Log.i("CANVAS", "CanvasRenderer reloadTexture error=", e); } } } } else if (m.type == FastCanvasMessage.Type.RENDER) { mRenderCommands = m.drawCommands; while (!mCaptureQueue.isEmpty()) { FastCanvasMessage captureMessage = mCaptureQueue.get(0); FastCanvasJNI.captureGLLayer(captureMessage.callbackContext.getCallbackId(), captureMessage.x, captureMessage.y, captureMessage.width, captureMessage.height, captureMessage.url); mCaptureQueue.remove(0); } } else if (m.type == FastCanvasMessage.Type.SET_ORTHO) { Log.i("CANVAS", "CanvasRenderer setOrtho width=" + m.width + ", height=" + m.height); FastCanvasJNI.setOrtho(m.width, m.height); } else if (m.type == FastCanvasMessage.Type.CAPTURE) { Log.i("CANVAS", "CanvasRenderer capture"); mCaptureQueue.add(m); } else if (m.type == FastCanvasMessage.Type.SET_BACKGROUND) { Log.i("CANVAS", "CanvasRenderer setBackground color=" + m.drawCommands); // Some validation of the background color string is // done in JS, but the format of m.drawCommands cannot // be fully validated so we're going to give this a shot // and simply fail silently if an error occurs in parsing try { int red = Integer.valueOf(m.drawCommands.substring(0, 2), 16); int green = Integer.valueOf(m.drawCommands.substring(2, 4), 16); int blue = Integer.valueOf(m.drawCommands.substring(4, 6), 16); FastCanvasJNI.setBackgroundColor(red, green, blue); } catch (Exception e) { Log.e("CANVAS", "Parsing background color: \"" + m.drawCommands + "\"", e); } } } }
From source file:com.kamosoft.flickr.model.Photo.java
private Bitmap getBitmapFromURL(String url) throws JSONException, IOException { Bitmap bm = null;//from w ww .j a v a2 s . c om URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(bis); bis.close(); is.close(); return bm; }
From source file:com.rumblefish.friendlymusic.api.WebRequest.java
public static Bitmap getBitmapAtURL(URL url) { InputStream inStream = null;//ww w .j ava 2s .co m HttpURLConnection _conn = null; Bitmap bitmap = null; try { _conn = (HttpURLConnection) url.openConnection(); _conn.setDoInput(true); _conn.connect(); inStream = _conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inStream); inStream.close(); _conn.disconnect(); inStream = null; _conn = null; } catch (Exception ex) { // nothing } if (inStream != null) { try { inStream.close(); } catch (Exception ex) { } } if (_conn != null) { _conn.disconnect(); } return bitmap; }
From source file:li.klass.fhem.fhem.FHEMWEBConnection.java
@Override public RequestResult<Bitmap> requestBitmap(String relativePath) { RequestResult<InputStream> response = executeRequest(relativePath, client, "request bitmap"); if (response.error != null) { return new RequestResult<Bitmap>(response.error); }// w w w. java 2 s .c o m Bitmap bitmap = BitmapFactory.decodeStream(response.content); return new RequestResult<Bitmap>(bitmap); }
From source file:com.radadev.xkcd.Comics.java
public static final Bitmap downloadBitmap(String url) { final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); final HttpGet getRequest = new HttpGet(url); try {// w ww.j av a 2 s .c om HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { InputStream inputStream = null; try { inputStream = new FlushedInputStream(entity.getContent()); final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } finally { if (inputStream != null) { inputStream.close(); } entity.consumeContent(); } } } catch (Exception e) { getRequest.abort(); } finally { if (client != null) { client.close(); } } return null; }
From source file:com.teleca.jamendo.util.download.DownloadTask.java
private static void downloadCover(DownloadJob job) { PlaylistEntry mPlaylistEntry = job.getPlaylistEntry(); String mDestination = job.getDestination(); String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination); File file = new File(path + "/" + "cover.jpg"); // check if cover already exists if (file.exists()) { Log.v(JamendoApplication.TAG, "File exists - nothing to do"); return;/*from w w w.j a v a 2 s .c o m*/ } String albumUrl = mPlaylistEntry.getAlbum().getImage(); if (albumUrl == null) { Log.w(JamendoApplication.TAG, "album Url = null. This should not happened"); return; } albumUrl = albumUrl.replace("1.100", "1.500"); InputStream stream = null; URL imageUrl; Bitmap bmp = null; // download cover try { imageUrl = new URL(albumUrl); try { stream = imageUrl.openStream(); bmp = BitmapFactory.decodeStream(stream); } catch (IOException e) { // TODO Auto-generated catch block Log.v(JamendoApplication.TAG, "download Cover IOException"); e.printStackTrace(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block Log.v(JamendoApplication.TAG, "download CoverMalformedURLException"); e.printStackTrace(); } // save cover to album directory if (bmp != null) { try { file.createNewFile(); OutputStream outStream = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream); outStream.flush(); outStream.close(); Log.v(JamendoApplication.TAG, "Album cover saved to sd"); } catch (FileNotFoundException e) { Log.w(JamendoApplication.TAG, "FileNotFoundException"); } catch (IOException e) { Log.w(JamendoApplication.TAG, "IOException"); } } }
From source file:com.entertailion.android.overlaynews.Downloader.java
private synchronized void updateFeeds() { Log.d(LOG_TAG, "updateFeeds"); updateTime = System.currentTimeMillis(); try {/*from w w w .j a v a 2 s .c o m*/ // iterate through feeds in database ArrayList<RssFeed> feeds = FeedsTable.getFeeds(context); Log.d(LOG_TAG, "feeds=" + feeds); if (feeds != null) { for (RssFeed feed : feeds) { // Check if TTL set for feed if (feed.getTtl() != -1) { if (System.currentTimeMillis() - (feed.getDate().getTime() + feed.getTtl() * 60 * 1000) < 0) { // too soon Log.d(LOG_TAG, "TTL not reached: " + feed.getTitle()); break; } } String rss = Utils.getRssFeed(feed.getLink(), context, true); RssHandler rh = new RssHandler(); RssFeed rssFeed = rh.getFeed(rss); if (rssFeed.getTitle() == null) { try { Uri uri = Uri.parse(feed.getLink()); rssFeed.setTitle(uri.getHost()); } catch (Exception e) { Log.e(LOG_TAG, "get host", e); } } Uri uri = Uri.parse(feed.getLink()); Log.d(LOG_TAG, "host=" + uri.getScheme() + "://" + uri.getHost()); String icon = Utils.getWebSiteIcon(context, "http://" + uri.getHost()); Log.d(LOG_TAG, "icon1=" + icon); if (icon == null) { // try base host address int count = StringUtils.countMatches(uri.getHost(), "."); if (count > 1) { int index = uri.getHost().indexOf('.'); String baseHost = uri.getHost().substring(index + 1); icon = Utils.getWebSiteIcon(context, "http://" + baseHost); Log.d(LOG_TAG, "icon2=" + icon); } } if (icon != null) { try { FileInputStream fis = context.openFileInput(icon); Bitmap bitmap = BitmapFactory.decodeStream(fis); fis.close(); rssFeed.setImage(icon); rssFeed.setBitmap(bitmap); } catch (Exception e) { Log.d(LOG_TAG, "updateFeeds", e); } } if (rssFeed.getBitmap() == null && rssFeed.getLogo() != null) { Log.d(LOG_TAG, "logo=" + rssFeed.getLogo()); Bitmap bitmap = Utils.getBitmapFromURL(rssFeed.getLogo()); if (bitmap != null) { icon = Utils.WEB_SITE_ICON_PREFIX + Utils.clean(rssFeed.getLogo()) + ".png"; Utils.saveToFile(context, bitmap, bitmap.getWidth(), bitmap.getHeight(), icon); rssFeed.setImage(icon); rssFeed.setBitmap(bitmap); } } // update database long time = 0; if (rssFeed.getDate() != null) { time = rssFeed.getDate().getTime(); } FeedsTable.updateFeed(context, feed.getId(), rssFeed.getTitle(), feed.getLink(), rssFeed.getDescription(), time, rssFeed.getViewDate().getTime(), rssFeed.getLogo(), rssFeed.getImage(), rssFeed.getTtl()); ItemsTable.deleteItems(context, feed.getId()); for (RssItem item : rssFeed.getItems()) { if (item.getTitle() != null) { time = 0; if (item.getDate() != null) { time = item.getDate().getTime(); } ItemsTable.insertItem(context, feed.getId(), item.getTitle(), item.getLink(), item.getDescription(), item.getContent(), time); } } // release resources Bitmap bitmap = rssFeed.getBitmap(); if (bitmap != null) { rssFeed.setBitmap(null); bitmap.recycle(); bitmap = null; } } } } catch (Exception e) { Log.e(LOG_TAG, "updateFeeds", e); } }
From source file:com.cpp255.bookbarcode.DouBanBookInfoXmlParser.java
public Bitmap DownloadBitmap(String bitmapUrl) { Bitmap bitmap = null;//from ww w . ja v a2s. com BufferedInputStream bis = null; try { URL url = new URL(bitmapUrl); URLConnection conn = url.openConnection(); bis = new BufferedInputStream(conn.getInputStream()); bitmap = BitmapFactory.decodeStream(bis); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) bis.close(); } catch (Exception e) { e.printStackTrace(); } } return bitmap; }
From source file:com.android.datacloud.Entity.java
/** * Devuelve un bitmap del recurso en la aplicacin * /*from w w w . j a va2 s .c o m*/ * @param name nombre del campo * @return valor (Tipo bitmap) */ public Bitmap getBitmap(String name) { int id = DataCloud.getInstance().getContext().getResources().getIdentifier( DataCloud.getInstance().getPackage() + ":drawable/" + getValue(name).toString(), null, null); java.io.InputStream is = DataCloud.getInstance().getContext().getResources().openRawResource(id); BitmapDrawable bmd = new BitmapDrawable(BitmapFactory.decodeStream(is)); return bmd.getBitmap(); }