I am retrieving an image from my SQLite table 开发者_运维问答and converting it to a byte array using this code :
byte[] imageByteArray = cImage.getBlob(cImage.getColumnIndex("ImageData"));
which works fine (I have proven this by decoding it back to the original image like this :
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
The problem I have is I need to serialise the image data and send it via .net web services, then decode it at the other end.
I am using this code to encode the byte[] to a Base64 string :
String sImageData = Base64.encode(imageByteArray);
Then adding it as a property to my service call.
The call takes a while to complete which indicates it is sending the data - although I get the exception "Value cannot be null" when I do this in the web service :
byte[] baImageData = Convert.FromBase64String(sImageData);
I'm not sure how I can debug this any further - am I missing something obvious?
it works fine, but if you send a long picture, it crash.
in 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());;
}
}
});
In .net Webservice:
<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
The android part looks fine to me.
精彩评论