2011-08-31 162 views
1

我從我的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); 

我不是當然,我可以如何進一步調試 - 我錯過了什麼明顯的東西?

回答

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

Android部分看起來很好。