Gunaseelan
Gunaseelan

Reputation: 15535

Send image to WCF from Android

This is my code from android

    public class UploadImage extends AsyncTask<Uri, Void, String> {

    @Override
    protected String doInBackground(Uri... params) {
        Bitmap bitmap;
        if (params[0] != null) {
            try {
                bitmap = BitmapFactory.decodeStream(getActivity()
                        .getContentResolver().openInputStream(params[0]));
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 10, out);
                DefaultHttpClient httpClient = new DefaultHttpClient();
                byte[] sendData = out.toByteArray();
                HttpPost httpPost = new HttpPost(URL + "/"
                        + METHODNAME_UPLOAD);
                httpPost.setHeader("Content-Type", "application/json");
                ByteArrayBody bab = new ByteArrayBody(sendData,
                        "mobile.png");
                MultipartEntity entity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);
                entity.addPart("image", bab);
                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost);
                System.out.println(response.getStatusLine().getStatusCode());
//              if (response.getStatusLine().getStatusCode() != 200) {
//                  throw new RuntimeException(
//                          "Failed : HTTP error code : "
//                                  + response.getStatusLine()
//                                          .getStatusCode());
//              } else {
//                  System.out.println("Success");
//              }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
}

And this is my WCF interface code

[OperationContract]
[WebInvoke(UriTemplate = "", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string UploadImage(Stream Image);

And this is my method,

public string UploadImage(Stream Image)
{
    return "Success";
}

I am getting status code 415 (Unsupported Media Type)

I have searched lot in google. Any help will be highly appreciable. Any other solutions also welcome. What I need is, I want to pick a image using ImagePicker and send it ti wcf service, From there I will upload to server.

Note: Alternate solution also welcome

Update 1 After @Vaishali answer I have updated my code as follows

bitmap = BitmapFactory.decodeStream(getActivity()
        .getContentResolver().openInputStream(params[0]));
byte[] sendData = getEncoded64ImageStringFromBitmap(bitmap);
ByteArrayBody bab = new ByteArrayBody(sendData,
        "mobile.png");
MultipartEntity entity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("mobile", bab);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL + "/"
        + METHODNAME_UPLOAD);
httpPost.setHeader("Content-Type", "application/json");
HttpResponse response = httpClient.execute(httpPost);
System.out
        .println(response.getStatusLine().getStatusCode());

And

public byte[] getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    byte[] byteFormat = stream.toByteArray();
    // get the base 64 string
    String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);

    return imgString.getBytes();
}

Now the status code is 400

Upvotes: 2

Views: 1273

Answers (4)

Jmocke
Jmocke

Reputation: 271

I will suggest that you send a base64 string then from the web service you convert the base64 string to byte[] and save it or get the image

Upvotes: 0

Vaishali Sutariya
Vaishali Sutariya

Reputation: 5121

  sendData = getEncoded64ImageStringFromBitmap(bitmap); 

  // here u got sendData is in string format .. pass this to  

   entity.addPart("imageNameWhateverUWantToPass", sendData);



 public static byte[] getEncoded64ImageStringFromBitmap(Bitmap bitmap) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 70, stream);
    byte[] byteFormat = stream.toByteArray();
    // get the base 64 string
    String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);

    return imgString.getBytes();
 }

Upvotes: 0

VVB
VVB

Reputation: 7641

415 is returned by the server when the entity sent in a request (content in a POST or PUT) has an unsupported mediatype.

Make sure you are sending the same media type which server is asking for.

Read this :

http://www.checkupdown.com/status/E415.html

Upvotes: 0

Brian
Brian

Reputation: 3713

I think you've got a mismatch in the data the WCF service is expecing (an IO stream) versus what it looks like you're sending (a byte array).

My experience has been that streams are difficult to get right in WCF, so I'd suggest changing your host-side interface to expect a byte array and see what happens.

Upvotes: 1

Related Questions