gpt4 book ai didi

android - 将图像从 Android 发送到 WCF

转载 作者:行者123 更新时间:2023-11-29 01:40:33 26 4
gpt4 key购买 nike

这是我的安卓代码

    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接口(interface)代码

[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(不支持的媒体类型)

我在谷歌搜索了很多。任何帮助将不胜感激。也欢迎任何其他解决方案。我需要的是,我想使用 ImagePicker 选择一个图像并将其发送到 wcf 服务,我将从那里上传到服务器。

注意: 也欢迎其他解决方案

更新 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

最佳答案

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

我的经验是流很难在 WCF 中正确处理,因此我建议更改您的主机端接口(interface)以期待一个字节数组,然后看看会发生什么。

关于android - 将图像从 Android 发送到 WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24951020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com