private InputStream handleNetworkURL(String path) throws IOException { InputStream is = null; Request request = new Request.Builder().url(path).build(); Response response = TiApplication.getOkHttpClientInstance().newCall(request).execute(); if (response.isSuccessful()) { InputStream lis = response.body().byteStream(); // we need to copy the stream because otherwise it will be closed // in the main thread which will throw :s ByteArrayOutputStream bos = null; try { bos = new ByteArrayOutputStream(8192); int count = 0; byte[] buf = new byte[8192]; while ((count = lis.read(buf)) != -1) { bos.write(buf, 0, count); } is = new ByteArrayInputStream(bos.toByteArray()); } catch (IOException e) { Log.e(TAG, "Problem pulling image data from " + path, e); throw e; } finally { if (lis != null) { try { lis.close(); lis = null; } catch (Exception e) { // Ignore } } if (bos != null) { try { bos.close(); bos = null; } catch (Exception e) { // ignore } } } } return is; }