Question: Since the Android developers [recommend](http://android-developers.blogspot.nl/2011/09/androids-http-clients.html) to use the `HttpURLConnection` class, I was wondering if anyone can provide me with a good example on how to send a bitmap "file" (actually an in-memory stream) via POST to an Apache HTTP server. I'm not interested in cookies or authentication or anything complicated, but I just want to have a reliable and logic implementation. All the examples that I've seen around here look more like "let's try this and maybe it works".

Right now, I have this code:

```
URL url;
HttpURLConnection urlConnection = null;
try {
    url = new URL("http://example.com/server.cgi");

    urlConnection = (HttpURLConnection) url.openConnection();

} catch (Exception e) {
    this.showDialog(getApplicationContext(), e.getMessage());
}
finally {
    if (urlConnection != null)
    {
        urlConnection.disconnect();
    }
}

```

where showDialog should just display an `AlertDialog` (in case of an invalid URL?).

Now, let's say that I generate a bitmap like so: `Bitmap image = this.getBitmap()` inside a control derived from `View` and I want to send it via POST. What would be the proper procedure to achieve such a thing? What classes do I need to use? Can I use `HttpPost` like in [this example](https://stackoverflow.com/a/4126746/1174378)? If so, how would I construct the `InputStreamEntity` for my bitmap? I would find it revolting to be required to first store the bitmap in a file on the device.

---

I should also mention that I really need to send every unaltered pixel of the original bitmap to the server, so I can't convert it to JPEG.

Answer: 
based on Mihai's solution, if anyone has the problem of saving images on the server like what happened on my server. change the Bitmap to bytebuffer part to : 

```
ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
        byte[] pixels = bos.toByteArray();

```
Here is what i did for uploading photo using post request.

```
public void uploadFile(int directoryID, String filePath) {
    Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    String upload_url = BASE_URL + UPLOAD_FILE;
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

    byte[] data = bao.toByteArray();

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(upload_url);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    try {
        // Set Data and Content-type header for the image
        FileBody fb = new FileBody(new File(filePath), "image/jpeg");
        StringBody contentString = new StringBody(directoryID + "");

        entity.addPart("file", fb);
        entity.addPart("directory_id", contentString);
        postRequest.setEntity(entity);

        HttpResponse response = httpClient.execute(postRequest);
        // Read the response
        String jsonString = EntityUtils.toString(response.getEntity());
        Log.e("response after uploading file ", jsonString);

    } catch (Exception e) {
        Log.e("Error in uploadFile", e.getMessage());
    }
}

```

**NOTE:** This code requires libraries so Follow the instructions [here](http://ogrelab.ikratko.com/using-newer-version-of-httpclient-like-4-1-x/) in order to get the libraries.