0

i am sending an image in byte[] and 3 strings to a webservice usingksoap but its not working for me , i am not sure where i am wrong, in sending image from Android and at receiving end, i am putting the code here kindly check it Here is how i am converting image to byte[] at client (Android) side

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

and here is the code where i am sending it to webservice via Ksoap

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Name", name);
request.addProperty("Email", email);
request.addProperty("Picture", encoded );
request.addProperty("Date", date);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport. call(SOAP_ACTION, envelope);

SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
String str = result.toString();

and here is the webMethod where i am receiving this soap envelop

  [WebMethod]
    public String PutFile(String Name, String Email, String Picture, String Date)
        {

String PictureByteString = Picture;
Image imgFromString = SaveByteArrayAsImage(PictureByteString);
DateTime.Now.ToShortDateString() + ".jpg"));
string serverpath = Server.MapPath("~/" + Email + "-" + DateTime.Now.ToShortDateString());
imgFromString.Save(serverpath, System.Drawing.Imaging.ImageFormat.Jpeg);
String Path = serverpath + ".Jpeg";


            return Name;
        }

private Image SaveByteArrayAsImage(string base64String)
{
byte[] bytes = Convert.FromBase64String(base64String);

image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}

return image;

}

when i send data to webservice android LogCat shows me

java.io.IOException: HTTP request failed, HTTP status: 500

i think so which means that the data i am sending to webservice is not of correct type, so i tried to make String Picture to byte[] Picture in webmethod but result was same. I am not being able to figure out where i am wrong ...

Update: now sending image in a Base64 string and java exception is gone but the webmethod is still not converting that Base64 string into image...

AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • 500 meance server not connecting you need to try first without passing image if it's work then pass image and to pass image if you send image in base64 formate then it's better then byte[] – PankajAndroid Mar 28 '14 at 05:07
  • let me try it without image.... – AddyProg Mar 28 '14 at 05:10
  • @PankajAndroid : I removed t this line "new MarshalBase64().register(envelope);" and added "String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);" to convert byte[] into b ase64 string , exception is gone now and webmethod is getting data properly but its not converting the string back to image.. please see the update in question – AddyProg Mar 28 '14 at 05:35

1 Answers1

0

This is how i did it.
parameter passed to function is Base64 string

public string SendImage(string data)
    {
        byte[] myarray = Convert.FromBase64String(data);
        MemoryStream memStream = new MemoryStream(myarray);
        Image myimage = Image.FromStream(memStream);
        myimage.Save("G:\\image.png", ImageFormat.Png);
        return "succeeded";
    }

This is working perfectly for me, Hope it helps.

  • I solved this issue a year ago :-P but thanks for the input it may help many others ... – AddyProg Jul 06 '15 at 07:48
  • Hahaha, i thought im pretty late too xD got into this just recently .. btw, any idea on how to actually send an image from .Net Webservice in the form of "C# Image" not byte array to android and vice versa ??! – Omar Sherif Mansour Jul 06 '15 at 13:55