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 8029301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:23:43+00:00 2026-06-05T00:23:43+00:00

I am trying to take a photo in an Android app, convert it to

  • 0

I am trying to take a photo in an Android app, convert it to JPEG, then upload it to a web service. So far I got the following:

private static final int CAMERA_REQUEST = 1888;
private void TakePhoto() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case CAMERA_REQUEST:
            Bitmap photo = (Bitmap) data.getExtras().get("data");

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 90, bos);
            byte [] bitmapData = bos.toByteArray();

            char [] dataAsChar = new String(bitmapData ).toCharArray();

            URL url = new URL("http://www.foo.com/bar);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/octet-stream");

            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(dataAsChar);
            wr.flush();

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                // Process line...
            }
            wr.close();
            rd.close();

            break;
    }
}

The itself seems to work, e.g. doesn’t crash. I get in the Bitmap photo variable. I then compress it using ByteArrayOutputStream and it becomes about 5103 bytes (in the buffer). I then convert it to a char array (so that it can be sent) and the char array becomes 4758 elements. Not sure why that is (probably this line char [] dataAsChar = new String(data).toCharArray();). And of course once the image makes over to the other side it’s corrupted.

What am I missing here?

EDIT. Per suggestion from @digitalJoel, I chucked the OutputStreamWriter and replaced with

OutputStream os = conn.getOutputStream();
os.write(data);
  • 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-05T00:23:45+00:00Added an answer on June 5, 2026 at 12:23 am

    new String(bitmapData ).toCharArray() seems like a really bad idea to me. I don’t think you can expect all the bytes from a JPEG to get shoved into a String without some corruption happening. So, why not just write the bytes to the OutputStream instead of wrapping it in the OutputStreamWriter?

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

Sidebar

Related Questions

I'm trying to take a photo from my iPhone app and upload to a
I am trying to upload some data to web service using post action But
I am trying to take a photo and post it on my facebook wall.
I am trying to take photos with my web cam, and I'm having some
I am trying to upload files to a web server using System.Net.WebClient.UploadFile but I
I'm having an issue where I'm using the camera app to take a photo
I am trying to make an app with a button that will take a
What I am currently trying to do is the following: Take a Picture, compress
I am trying to use the following code to take screen shots from my
I'm trying to get a basic image picker/photo taker running in my app and

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.