Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8338869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:43:27+00:00 2026-06-09T04:43:27+00:00

Since the Android developers recommend to use the HttpURLConnection class, I was wondering if

  • 0

Since the Android developers recommend 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? 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:43:29+00:00Added an answer on June 9, 2026 at 4:43 am

    I have no idea why the HttpURLConnection class does not provide any means to send files without having to compose the file wrapper manually. Here’s what I ended up doing, but if someone knows a better solution, please let me know.

    Input data:

    Bitmap bitmap = myView.getBitmap();
    

    Static stuff:

    String attachmentName = "bitmap";
    String attachmentFileName = "bitmap.bmp";
    String crlf = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    

    Setup the request:

    HttpURLConnection httpUrlConnection = null;
    URL url = new URL("http://example.com/server.cgi");
    httpUrlConnection = (HttpURLConnection) url.openConnection();
    httpUrlConnection.setUseCaches(false);
    httpUrlConnection.setDoOutput(true);
    
    httpUrlConnection.setRequestMethod("POST");
    httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
    httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
    httpUrlConnection.setRequestProperty(
        "Content-Type", "multipart/form-data;boundary=" + this.boundary);
    

    Start content wrapper:

    DataOutputStream request = new DataOutputStream(
        httpUrlConnection.getOutputStream());
    
    request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
    request.writeBytes("Content-Disposition: form-data; name=\"" +
        this.attachmentName + "\";filename=\"" + 
        this.attachmentFileName + "\"" + this.crlf);
    request.writeBytes(this.crlf);
    

    Convert Bitmap to ByteBuffer:

    //I want to send only 8 bit black & white bitmaps
    byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight()];
    for (int i = 0; i < bitmap.getWidth(); ++i) {
        for (int j = 0; j < bitmap.getHeight(); ++j) {
            //we're interested only in the MSB of the first byte, 
            //since the other 3 bytes are identical for B&W images
            pixels[i + j] = (byte) ((bitmap.getPixel(i, j) & 0x80) >> 7);
        }
    }
    
    request.write(pixels);
    

    End content wrapper:

    request.writeBytes(this.crlf);
    request.writeBytes(this.twoHyphens + this.boundary + 
        this.twoHyphens + this.crlf);
    

    Flush output buffer:

    request.flush();
    request.close();
    

    Get response:

    InputStream responseStream = new 
        BufferedInputStream(httpUrlConnection.getInputStream());
    
    BufferedReader responseStreamReader = 
        new BufferedReader(new InputStreamReader(responseStream));
    
    String line = "";
    StringBuilder stringBuilder = new StringBuilder();
    
    while ((line = responseStreamReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
    }
    responseStreamReader.close();
    
    String response = stringBuilder.toString();
    

    Close response stream:

    responseStream.close();
    

    Close the connection:

    httpUrlConnection.disconnect();
    

    PS: Of course I had to wrap the request in private class AsyncUploadBitmaps extends AsyncTask<Bitmap, Void, String>, in order to make the Android platform happy, because it doesn’t like to have network requests on the main thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I needed to build an android SDK that other developers can integrate into
I noticed that the Android Developers Activity section has been updated since I started
Since Android canvas is not hardware accelerated (untill 3), I want to use OpenGL
-webkit-device-pixel-ratio query is supported by both iOS and Android but since iOS does not
Since installing v17 of the android build tools I am getting a VerifyError in
Since the ActionBar is available only in Android 3.0 and later, what is a
I have installed Android SDK and packages. Since I had an error when opening
I am working on an application for android and we since we have lots
Those who invented Android and Java didn't invented for themselves. Since I started developing
I have a big big problem. Yesterday I updated the Android SDK and, since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.