Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created August 22, 2011 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mryoshio/1161769 to your computer and use it in GitHub Desktop.
Save mryoshio/1161769 to your computer and use it in GitHub Desktop.
get image and process into byte[]
private byte[] requestImage(String path) throws ServerConnectionException {
BufferedInputStream inStream = null;
try {
HttpResponse response = client.execute(buildGetRequest(
HttpGet.METHOD_NAME, path, new HashMap()));
HttpEntity entity = response.getEntity();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (entity == null) {
return new byte[0];
} else {
inStream = new BufferedInputStream(entity.getContent());
byte[] bytes = new byte[1024];
int b = 0;
while ((b = inStream.read(bytes)) != -1) {
outputStream.write(bytes);
}
outputStream.flush();
outputStream.close();
return outputStream.toByteArray();
}
} catch (Exception e) {
throw new ServerConnectionException(e);
} finally {
if (inStream != null)
try {
inStream.close();
} catch (IOException e) {
throw new ServerConnectionException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment