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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:58:09+00:00 2026-06-13T21:58:09+00:00

I have a strange issue with writing a bitmap stream to WCF DataOutputStream request.

  • 0

I have a strange issue with writing a bitmap stream to WCF DataOutputStream request. I am getting:

IOException: expected bytes XXX but received XXX

I have tested it with FileInputStream and it’s uploading perfectly, but when I convert the bitmap to an InputStream it throws an IOException. Any help will be highly appreciated.

Below is my function with which I call an AsyncTask:

public static Boolean AddAdItemImage(int AdItemId,File ThisFile)
{
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    InputStream fileInputStream = null;
    ByteArrayOutputStream stream = null;

    try 
    {    
        if (ThisFile.isFile()) 
        {
            Bitmap Bm = Utility.decodeSampledBitmapFromResource(ThisFile.getPath(), 640, 480);
            stream = new ByteArrayOutputStream();
            Bm.compress(CompressFormat.JPEG, 100, stream);
            fileInputStream = new ByteArrayInputStream(stream.toByteArray());

            //fileInputStream = new FileInputStream(ThisFile);

            conn = (HttpURLConnection) new URL(Params.GetServiceUrl()+"/AddAdImage?AdItemId="+String.valueOf(AdItemId)+"&ContentType="+Utility.GetFileMimeType(ThisFile)+"&apikey="+Params.GetApiKey()).openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");

            conn.setFixedLengthStreamingMode((int) ThisFile.length());// works fine till 24 mb file
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type","application/stream");

            dos = new DataOutputStream(conn.getOutputStream());

            int maxBufferSize =  1 * 1024 * 1024;
            int bytesAvailable = fileInputStream.available();
            int bufferSize = Math.min(bytesAvailable,maxBufferSize);

            byte[] buffer = new byte[bufferSize];

            Log.i("Upload Ad Image", "Writing Stream");

            while ((bufferSize = fileInputStream.read(buffer)) > 0)
            {
                dos.write(buffer, 0, bufferSize);
            }

            Log.i("Upload Ad Image", "Stream Written");

            //dos.write(stream.toByteArray());

            String lineEnd = "";

            dos.writeBytes(lineEnd);

            Log.i("Upload Ad Image", "Line Written");

            // read the SERVER RESPONSE
            inStream = new DataInputStream(conn.getInputStream());
            String str = new String();
            String strResponse = new String();

            while ((str = inStream.readLine()) != null) 
            {
                strResponse += str;
            }

            inStream.close();

            dos.flush();
            dos.close();

            return Boolean.valueOf(strResponse);
        } 
    }
    catch (MalformedURLException ex)
    {
        Log.e("AddAdItemImage", "MalformedURLException: " + ex.getMessage(),ex);
    } 
    catch (IOException ioe)
    {
        Log.e("AddAdItemImage", "IOException: " + ioe.getMessage(),ioe);
    }
    catch (IllegalStateException e)
    {
        Log.e("AddAdItemImage", "IllegalStateException: " + e.getMessage(),e);
    }
    catch (Exception e)
    {
        Log.e("AddAdItemImage", "Exception: " + e.getMessage(),e);
    }
    finally
    {
        try
        {
            if(fileInputStream != null)
                fileInputStream.close();
        }
        catch (IOException e){} 

        try 
        {   
            if(stream!=null)
                stream.close();
        }
        catch (IOException e) {}
    }   

    return false;
}
  • 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-13T21:58:10+00:00Added an answer on June 13, 2026 at 9:58 pm

    I solved the problem, it was this line:

    conn.setFixedLengthStreamingMode((int) ThisFile.length()); 
    

    After file content length was different than the newly created bitmap. I wonder how I could spend almost two days to find the solution; I guess sometimes you just need to think out of the box 😀

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

Sidebar

Related Questions

I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but
I'm having a strange issue that I think I have a workaround for, but
I have a strange issue with a MVC 3 app running as an Azure
I have a strange issue . I am currently working on a mail app
i have a strange issue with phpexcel on a pair of formulas and can't
I have a strange issue: When I scroll vertically it triggers swipe right/left events.
I have a strange issue (at least for me :)) with the MySQL's locking
I have a strange issue that has arisen recently: Whenever I enter text, even
I have a strange issue with jQuery's hover, addClass $(document).ready(function(){ $('#selectable li').hover( function(){ $(this).addClass('selecting',
I have a strange issue with eclipse. When I put a .xls file in

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.