Syed Waqas
Syed Waqas

Reputation: 862

Android can not write newly created bitmap stream to WCF service

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;
}

Upvotes: 1

Views: 362

Answers (1)

Syed Waqas
Syed Waqas

Reputation: 862

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 :D

Upvotes: 2

Related Questions