1

我再次写它,我正在开发一个应用程序,用户通过图像单击图片,图像将存储在 sdcard 中,然后我从图像视图中选择图片并将该图片发送到服务器。我已经将我的图像转换为字节数组,然后以 Base64 字符串格式对其进行编码,但是当我尝试将该图像发送到服务器时,它给了我一个肥皂错误错误。这是我的android设备代码:

公共无效完成图像(){

    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 网络服务。代码是:

_ 公共函数 UploadImage(ByVal imgdata As String, ByVal FileName As String) As Integer

    '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 天开始一​​直在努力。任何帮助表示赞赏。并提前致谢......

4

1 回答 1

1

休息会更好地从android调用服务。无论如何,如果您想以自己的方式执行此操作,则可以将流发送到您的服务,请创建一个采用流的方法。并且您的 android 端询问输出流的连接并将所有字节写入其中。

我已经用 Restful wcf 做到了这一点。供参考: 将 MS Word 文件从 Android 上传到 .Net WCF?

于 2012-04-19T13:28:58.963 回答