我正在尝试使用下面的代码使用改装2发送图像,但我收到的图像已损坏,无法使用Windows照片查看器打开。你有两个截图,第一个是我被解析为字符串的内容,第二个是我尝试打开图像时得到的消息。 我已经尝试了不同的选项来读取已发送图像的流,但没有一个工作。一个是wcf中的代码我使用的另外两个选项是注释的代码,但它们都不起作用。希望有人能帮助我。
@Multipart
@POST("PostImageJSON")
Call<String> PostImage(@Part MultipartBody.Part report_image);
MediaType imageType = MediaType.parse("image/*");
File file = new File(ImagePath+"/savedReportPhoto.jpg");
if(file.exists()) {
RequestBody requestBodyImage = RequestBody.create(imageType, file);
MultipartBody.Part image = MultipartBody.Part.createFormData("uploadedImage", file.getName(), requestBodyImage);
Call<String> callSendImage = api.PostImage(image);
progress.setTitle("Procesim");
progress.setMessage("Duke procesuar..");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
callSendImage.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
String responseimg = response.body();
progress.dismiss();
} else {
Toast.makeText(getApplicationContext(), "Something went wrong with the request to the server !", Toast.LENGTH_SHORT).show();
progress.dismiss();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
progress.dismiss();
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/PostImageJSON", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
String PostImage(Stream postedImage);
public String PostImage(Stream postedImage)
{
//Here i parse the stream as string for testing purposes
StreamReader reader = new StreamReader(postedImage);
string res = reader.ReadToEnd();
using (StreamWriter writer = new StreamWriter("F:\\image.txt", true))
{
writer.WriteLine(res);
}
//First option i have tried
//do
//{
// bytesRead = postedImage.Read(buffer, 0, buffer.Length);
// totalBytesRead += bytesRead;
// ms.Write(buffer, 0, bytesRead);
//} while (bytesRead > 0);
//using (FileStream fs = File.OpenWrite("F:\\postedImage.png"))
//{
// ms.WriteTo(fs);
// fs.Close();
// ms.Close();
//}
//Second option i have tried
byte[] buffer = new byte[19127];
postedImage.Read(buffer, 0, 19127);
FileStream f = new FileStream("F:\\savedReportPhoto.jpg", FileMode.OpenOrCreate);
f.Write(buffer, 0, buffer.Length);
f.Close();
postedImage.Close();
return "Recieved the image on server";
//Third option i have tried
//FileStream f = new FileStream("F:\\savedReportPhoto.jpg", FileMode.OpenOrCreate);
//postedImage.CopyTo(f);
//f.Close();
//postedImage.Close();
//return "Recieved the image on server";
}
答案 0 :(得分:0)
好的我在经过一周的努力后找到了解决方案,解决方案是不将图像与其他数据分开,而是将其编码为base64字符串并将其与对象一起发送,然后在服务器中对其进行解码做你想做的事。
这是改造界面中的方法:
@POST("PostBanderolDataJSON")
Call<ReportPostData> PostData(@Header("Authorization") String serviceToken,@Body ReportPostData reportdata);
在这里我发送数据以及编码图像:
ReportPostData rpd = new ReportPostData();
rpd.setBanderolnr(banderoNr);
rpd.setPharmacy(pharmacy);
rpd.setPlace(place);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bmp = Common.loadImageFromStorage(ImagePath);
if(bmp!=null) {
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = android.util.Base64.encodeToString(imageBytes, Base64.DEFAULT);
rpd.setImageFile(encodedImage);
}
然后将图像添加到对象中,如上所示:
rpd.setImageFile(encodedImage);
并将对象加入,并像往常一样通过改造发送。
希望有人找到这个有用的