android映像到.net webservice

时间:2011-08-31 16:30:25

标签: android .net web-services image bytearray

我正从我的SQLite表中检索图像并使用以下代码将其转换为字节数组:

byte[] imageByteArray = cImage.getBlob(cImage.getColumnIndex("ImageData")); 

工作正常(我通过将其解码回原始图像证明了这一点:

ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);

我遇到的问题是我需要序列化图像数据并通过.net网络服务发送,然后在另一端解码。

我正在使用此代码将byte []编码为Base64字符串:

String sImageData = Base64.encode(imageByteArray);

然后将其作为属性添加到我的服务电话中。

调用需要一段时间才能完成,这表明它正在发送数据 - 但是当我在Web服务中执行此操作时,我得到异常“Value not not null”:

byte[] baImageData = Convert.FromBase64String(sImageData);

我不确定如何进一步调试 - 我错过了一些明显的东西吗?

2 个答案:

答案 0 :(得分:0)

android部分看起来很好。

答案 1 :(得分:0)

它工作正常,但是如果你发送一张长图,就会崩溃。

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 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