从Android发送图像到WCF

时间:2014-07-25 08:11:15

标签: android wcf

这是我的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;
    }
}

这是我的WCF接口代码

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

这是我的方法,

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

我收到状态代码415 (Unsupported Media Type)

我在谷歌搜索了很多。任何帮助都会非常值得一提。任何其他解决方案也欢迎。我需要的是,我想使用ImagePicker选择一个图像并发送给服务员,然后我将上传到服务器。

注意: 也欢迎替代解决方案

更新1 在@Vaishali回答之后我更新了我的代码如下

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());

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

现在状态代码为400

4 个答案:

答案 0 :(得分:1)

我认为您在WCF服务预期的数据(IO流)与您发送的数据(字节数组)之间存在不匹配。

我的经验是,在WCF中很难正确地使用流,因此我建议更改主机端接口以期望一个字节数组,看看会发生什么。

答案 1 :(得分:0)

当请求中发送的实体(POST或PUT中的内容)具有不受支持的媒体类型时,服务器返回

415。

确保您发送的服务器要求的媒体类型相同。

阅读本文:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我建议您从web服务发送base64字符串,然后将base64字符串转换为byte []并保存或获取图像