2012-04-19 132 views
1

我正在寫它,我正在開發一個應用程序,其中用戶通過圖像單擊圖片和圖像將存儲在SD卡中,然後我從圖像視圖中選擇圖片並將該圖片發送到服務器。我已經將我的圖像轉換爲字節數組,然後將其編碼爲Base64字符串格式,但是當我嘗試將該圖像發送給服務器時,它會給我一個soap故障錯誤。下面是Android設備的我的代碼:)如何解決在服務器上上傳圖片的錯誤?

公共無效doneImage({

ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    bitmap.compress(CompressFormat.JPEG, 100, out); 
    byte[] imagebyte = out.toByteArray(); 

    String jp = caption.getText().toString();//for fetching the path of selected image path.// 

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME); 
    String strBase64 = Base64.encode(imagebyte); 

    PropertyInfo addProp = new PropertyInfo(); 
    addProp.setName("imgdata"); 
    addProp.setValue(strBase64); 
    addProp.setType(String.class); 
    Request.addProperty(addProp); 


    PropertyInfo addProp1 = new PropertyInfo(); 
    addProp1.setName("FileName"); 
    addProp1.setValue(jp); 
    addProp1.setType(String.class); 
    Request.addProperty(addProp1); 


    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    soapEnvelope.dotNet=true; 
    soapEnvelope.setOutputSoapObject(Request); 

    HttpTransportSE aht=new HttpTransportSE(URL); 

     try 
     { 
     aht.call(SOAP_ACTION, soapEnvelope); 
     SoapPrimitive response = (SoapPrimitive)soapEnvelope.getResponse(); 
     Toast.makeText(getApplicationContext(), "Success" +response, Toast.LENGTH_LONG).show(); 
     } 
     catch (Exception e) { 
      if (dialog.isShowing()) 
       dialog.dismiss(); 
       dialog.cancel(); 
      Toast.makeText(getApplicationContext(), 
        e.toString(), 
        Toast.LENGTH_LONG).show(); 
      caption.setText(e.toString()); 
      //return null; 
      //Log.e(e.getClass().getName(), e.getMessage(), e); 

     } 
} 




    @Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     switch (requestCode) { 
    case PICK_IMAGE: 
    if (resultCode == Activity.RESULT_OK) { 
     Uri selectedImageUri = data.getData(); 

     String filePath = null; 

     try { 
     // OI FILE Manager 
     String filemanagerstring = selectedImageUri.getPath(); 

     // MEDIA GALLERY 
     String selectedImagePath = getPath(selectedImageUri); 

     if (selectedImagePath != null) { 
     filePath = selectedImagePath; 

     caption.setText(selectedImagePath); 

     } else if (filemanagerstring != null) { 
     filePath = filemanagerstring; 

     caption.setText(filemanagerstring); 
      } else { 
      Toast.makeText(getApplicationContext(), "Unknown path", 
          Toast.LENGTH_LONG).show(); 
      Log.e("Bitmap", "Unknown path"); 
       } 

    if (filePath != null) { 
     decodeFile(filePath); 
    } else { 
     bitmap = null; 
     } 
    } catch (Exception e) { 
     Toast.makeText(getApplicationContext(), "Internal error", 
         Toast.LENGTH_LONG).show(); 
     Log.e(e.getClass().getName(), e.getMessage(), e); 
     } 
     } 
     break; 
    default: 
    } 
} 

,並在服務器端我用vb.net web服務。和代碼是:

_ 公共職能UploadImage(BYVAL imgdata作爲字符串,BYVAL文件名作爲字符串)作爲整數

'Find the path on the server of our apps images folder 
    Dim FilePath As String = Server.MapPath("images") 

    'strip the path and .jpg etc out of our filename 
    Dim i As Integer = InStrRev(FileName, "\") 
    FileName = Mid(FileName, i) 
    Dim j As Integer = InStr(FileName, ".") - 1 
    Dim k As Integer = Len(FileName) 
    FileName = Mid(FileName, 1, j) 

    'Storing an image to bitmap after converting from base64 format' 
    Dim Bm As New System.Drawing.Bitmap(Base64ToImage(imgdata)) 

    'Convert the bitmap to a png and save it in our images folder 
    BM.Save(FilePath & FileName, Imaging.ImageFormat.Jpeg) 

    'eventually we will create a database entry for this image and return the image ID 
    Return 1 
End Function 

'For converting the base64 satring value into the image' 
Public Function Base64ToImage(ByVal base64String As String) As Image 
    ' Convert Base64 String to byte[] 
    Dim imageBytes As Byte() = Convert.FromBase64String(base64String) 
    Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length) 

    ' Convert byte[] to Image 
    ms.Write(imageBytes, 0, imageBytes.Length) 
    Dim image__1 As Image = Image.FromStream(ms, True) 
    Return image__1 
End Function 

請人幫助 - 我出去,我的工作就可以了從持續4天。 任何幫助被讚賞。並感謝提前......

回答

1

休息會更好地從android調用服務。 反正如果你想這樣做你的方式你可以發送流到你的服務做一個方法,採取流。 和你的android端問連接輸出流並寫入所有字節。

我已經用Restful wcf完成了這項工作。 僅供參考: Uploading MS Word files from Android to .Net WCF?