If it's possible to post in original image format then send data directly from file stream:
FileInputStream imageIputStream = new FileInputStream(image_file); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); OutputStream out = connection.getOutputStream(); copyStream(imageIputStream, out); out.close(); imageIputStream.close();
copyStream function:
static int copyStream(InputStream src, OutputStream dst) throws IOException { int read = 0; int read_total = 0; byte[] buf = new byte[1024 * 2]; while ((read = src.read(buf)) != -1) { read_total += read; dst.write(buf, 0, read); } return (read_total); }