到.NET Web服务
问题描述:
我从我的SQLite表中检索图像,并使用此代码将其转换为字节数组的Android图像:到.NET Web服务
byte[] imageByteArray = cImage.getBlob(cImage.getColumnIndex("ImageData"));
工作正常(我已经通过解码回的证明了这原始图像是这样的:
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
我已经是我需要连载的图像数据,并通过.NET Web服务发送,那么它在另一端进行解码的问题
我用这个。将字节[]编码为Base64字符串的代码:
String sImageData = Base64.encode(imageByteArray);
然后将其作为属性添加到我的服务调用中。
呼叫需要一段时间才能完成,这表明它正在发送数据 - 虽然我得到的异常“值不能为空”我这样做是Web服务时:
byte[] baImageData = Convert.FromBase64String(sImageData);
我不是当然,我可以如何进一步调试 - 我错过了什么明显的东西?
答
它工作正常,但如果您发送长图片,它会崩溃。
Android中:
private static final String NAMESPACE = "http://tempuri.org/";
private static String URL="http://XXX.XX.113.79/Sincro_Mobile/Sincro.asmx";
ws_btn_Enviar_foto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final String METHOD_NAME = "FOTO_and";
final String SOAP_ACTION ="http://tempuri.org/FOTO_and";
ws_resultado.setText("");
request = new SoapObject(NAMESPACE, METHOD_NAME);
Bitmap imagen_decodificar = BitmapFactory.decodeFile (Environment.getExternalStorageDirectory().getAbsolutePath()+
"/"+Globales.fotos_sgi+"/tomafoto1_resize_70.jpg");
ByteArrayOutputStream out = new ByteArrayOutputStream();
imagen_decodificar.compress(CompressFormat.JPEG, 70, out);
byte[] imagebyte = out.toByteArray();
String strBase64 = Base64.encodeBytes(imagebyte);
request.addProperty("user", "ap");
request.addProperty("pass", "qwerty");
request.addProperty("x", contrasena);
request.addProperty("buffer",strBase64);
request.addProperty("filename","primer_foto.jpg");
//request.addProperty("Fecha_MOVIMIENTOS_y_FOTOS",dia_de_hoy.toString());
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; //se asigna true para el caso de que el WS sea de dotNet
envelope.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try {
transporte.call(SOAP_ACTION, envelope);
resultsRequestSOAP = (SoapPrimitive)envelope.getResponse();
ws_resultado.setText(resultsRequestSOAP.toString()) ;
}
catch (IOException e) {
ws_resultado.setText(e.toString());;
}
catch (XmlPullParserException e) {
ws_resultado.setText(e.toString());;
}
}
});
在NET Web服务:
<WebMethod()> Public Function FOTO_and(ByVal user As String, ByVal pass As String, _
ByVal x As String, ByVal buffer As Byte(), ByVal filename As String) As String
Dim Log As String = Validacion_User_EMEI(user, pass, x)
If Log <> Estados_Sincro.OK Then
Return Log
End If
'--VERIFICA QUE EXISTA LA CARPETA PARA ALMACENAR LA FOTO
Dim Path_Fotos As String
Path_Fotos = AppSettings.GetValues("Fotos")(0) + Now.ToString("yyMMdd")
If Directory.Exists(Path_Fotos) = False Then
Directory.CreateDirectory(Path_Fotos)
End If
'---VERIFICA QUE LA FOTO NO EXISTA
If File.Exists(Path_Fotos + "\" + filename) Then
Kill(Path_Fotos + "\" + filename)
End If
'--GUARDA LA FOTO
Dim binWriter As New BinaryWriter(File.Open(Path_Fotos + "\" + filename, _
FileMode.CreateNew, FileAccess.ReadWrite))
binWriter.Write(buffer)
binWriter.Close()
Return Estados_Sincro.OK
End Function
答
Android部分看起来很好。