我正在开发一个 Android 应用程序,它应该将图像发送到我的 ASP.NET Web 服务,图像将保存为文件。我已经看到了几种方法可以做到这一点,我选择了这种方法:将图像转换为字节数组 -> 将字节数组转换为字符串 -> 使用 KSOAP2 将字符串发送到 Web 服务 -> 接收字符串在 Web 服务 -> 将其转换为字节数组 ->将其另存为图像:
IVtest = (ImageView)findViewById(R.id.carticon);
BitmapDrawable drawable = (BitmapDrawable) IVtest.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
ImageView image=new ImageView(this);
image.setImageBitmap(bmp);
String strBase64 = Base64.encode(data);
然后我将 strBase64 发送到 Web 服务。
在网络服务中我有这个:
public Image ConvertToImage(byte[] image)
{
MemoryStream ms = new MemoryStream(image);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
[WebMethod]
public String UploadImage(String image, String name)
{
byte[] image_byte = Encoding.Unicode.GetBytes(image);
Image convertedImage = ConvertToImage(image_byte);
try {
convertedImage.Save(Server.MapPath("generated_image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
} catch (Exception e) {
return e.Message;
}
return "Success";
}
我收到一个错误line: Image returnImage = Image.FromStream(ms);
这是我得到的错误:
SoapFault - faultcode: 'soap:Server' faultstring: 'System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
at Service.ConvertToImage(Byte[] image) in e:\FTP\nvm\Service.asmx:line 1366
at Service.UploadImage(String image, String name) in e:\FTP\nvm\Service.asmx:line 1374
--- End of inner exception stack trace ---' faultactor: 'null' detail: org.kxml2.kdom.Node@437bf7b0
Thanks