从库中选择图像,我可以上传流,但得到错误"参数无效".在尝试转换为WCF中的图像时.
Android代码创建与REST服务的连接,并将图像上传为Byte []:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 6, out);
DefaultHttpClient httpClient = new DefaultHttpClient();
byte[] sendData = out.toByteArray();
HttpPost postRequest = new HttpPost("http://www.thehost.dk/MobileService/Service.svc/uploadpicture/");
ByteArrayBody bab = new ByteArrayBody(sendData, "picture.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", bab);
postRequest.setEntity(reqEntity);
httpClient.execute(postRequest);
Run Code Online (Sandbox Code Playgroud)
WCF C#代码接收流并尝试转换为图像,但收到错误:
public void UploadPicture(Stream imageData)
{
try
{
byte[] image = StreamToBytes(imageData);
ImageConverter imageConverter = new ImageConverter();
Image img = imageConverter.ConvertFrom(image) as Image; <-- Exception happens here
SaveImage(img);
}
catch (Exception ex)
{
Log(ex.ToString());
}
}
private byte[] StreamToBytes(Stream stream)
{
byte[] output = new byte[0];
byte[] stream_array = new byte[0];
byte[] buffer = new byte[1024];
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
stream_array = new byte[output.Length + read];
output.CopyTo(stream_array, 0);
Array.Copy(buffer, 0, stream_array, output.Length, read);
output = stream_array;
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
小智 5
我想首先解释为什么问题首先发生然后给你解决方案:在你的代码上尝试这个实验.而不是您使用的以下代码,将其替换为下面的代码:
public void UploadPicture(Stream imageData)
{
try
{
byte[] image = StreamToBytes(imageData);
ImageConverter imageConverter = new ImageConverter();
Image img = imageConverter.ConvertFrom(image) as Image;
SaveImage(img);
}
catch (Exception ex)
{
Log(ex.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
用...来代替,
public void UploadPicture (Stream imageData)
{
try
{
byte[] buffer = new byte[10000];
imagedata.Read(buffer, 0, 10000);
FileStream f = new FileStream("D:\\FileUpload\\SubjectFront.JPG", FileMode.OpenOrCreate);
f.Write(buffer, 0, buffer.Length);
f.Close();
imagedata.Close();
}
catch (Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud)
此代码执行时没有任何异常(我相信),您会在"已保存"路径中找到该图像文件.注意在D:\ FileUpload中创建的文件.文件的大小远大于您的Android代码发送的文件的大小(即,如果您还没有压缩它).尝试使用任何图像编辑器打开此文件,它将作为损坏的文件打开(您将无法看到图像).
现在用记事本打开文件.可以看到,除了乱码二进制数据外,还有一些文本可用.示例: - 内容 - 处置:表格数据; ......等等等等.此文本是"参数无效"异常的罪魁祸首.在java中使用Multipart时,它会添加此文本并将其发送到转换为字节流的图像数据.我不知道为什么会这样.现在,删除任何人类可读的文本并保存文件.在任何图像编辑器和中提琴中打开文件,都有你的图像.
现在,解决方案是编写代码以从WCF方法接收的流中删除此文本.我找到了一个非常漂亮的解析器(感谢Anthony),它有效地完成了工作.你可以在这里找到它(http://multipartparser.codeplex.com/).只需将此.cs文件添加到您的项目中,并执行以下操作.
public string UploadPicture (Stream imagedata)
{
MultipartParser parser = new MultipartParser(imagedata);
if (parser.Success)
{
string fileName = parser.Filename;
string contentType = parser.ContentType;
byte[] fileContent = parser.FileContents;
System.Drawing.Image image = byteArrayToImage(fileContent);
image.Save("D:\\FileUpload\\" + fileName);
return "Success !!!";
}
else
{
return "Exception!!!";
}
}
Run Code Online (Sandbox Code Playgroud)
byteArrayToImage的实现如下:
public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
return returnImage;
}
Run Code Online (Sandbox Code Playgroud)
希望它有所帮助......
| 归档时间: |
|
| 查看次数: |
4436 次 |
| 最近记录: |