private Bitmap downloadBitmap(String url) { Bitmap ret = null; InputStream s = null; try { if (url.startsWith("/")) { ret = MediaUtils.readFileBitmap(url, true); if (ret != null) return ret; } Logger.LogDebug("Trying to download " + url); HttpURLConnection uc = (HttpURLConnection) new URL(url).openConnection(); uc.setConnectTimeout(15000); uc.connect(); publishProgress(0); if (uc.getResponseCode() >= 400) throw new IOException(uc.getResponseCode() + " on " + url); Integer length = uc.getContentLength(); Logger.LogInfo("Response received. " + length + " bytes."); publishProgress(-2); s = new BufferedInputStream(uc.getInputStream(), WallChanger.DOWNLOAD_CHUNK_SIZE); ByteArrayBuffer bab = new ByteArrayBuffer(length <= 0 ? 32 : length); byte[] b = new byte[WallChanger.DOWNLOAD_CHUNK_SIZE]; int read = 0; int position = 0; while ((read = s.read(b, 0, WallChanger.DOWNLOAD_CHUNK_SIZE)) > 0) { position += read; bab.append(b, 0, read); publishProgress(position, length > position ? length : position + s.available()); } b = bab.toByteArray(); MediaUtils.writeFile(url.substring(url.lastIndexOf("/") + 1), b, true); ret = BitmapFactory.decodeByteArray(b, 0, b.length); } catch (IOException ex) { Logger.LogError("Couldn't download base image. " + url, ex); } finally { try { if (s != null) s.close(); } catch (IOException ex) { Logger.LogError("Error closing stream while downloading base image.", ex); } } return ret; }