Michiru
Michiru

Reputation: 31

Send an image from Android using ksoap2 to a webservice

I want to send an image using ksoap2 library image captured with a camera:

public class testwebservice extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION="http://axis2.soap.webservice.vogella.de/getNumberResponse";
    private static final String METHOD_NAME="getNumberResponse";
    private static final String NAMESPACE="http://axis2.soap.webservice.vogella.de/";
    private static final String URL="http://192.168.0.178:8080/Randomnumber/services/RandomNumber.RandomNumberHttpEndpoint/";

    ImageView imv;
    TextView tv;
    Bitmap bmp;
    int i;
    final static int CAMERA_RESULT = 0;
    Button upload;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.text1);

        upload = (Button)findViewById(R.id.upload);

        upload.setOnClickListener(this);
        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(i,  CAMERA_RESULT);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK)
        {
            Bundle extras = intent.getExtras();
            bmp = (Bitmap) extras.get("data");
            imv = (ImageView) findViewById(R.id.ReturnedImageView);
            imv.setImageBitmap(bmp);
        }
    }
}

Then I have an AlertDialog which asks me if I want to send the image or not, then I call the actual function that transforms the image using Marshallbase64:

public void testWebService() {
    MarshalBase64 b = new MarshalBase64();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, out);
    byte[] imagebyte = out.toByteArray();

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    Request.addProperty("image",imagebyte);

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

    b.register(soapEnvelope);
    HttpTransportSE aht=new HttpTransportSE(URL);

    try
    {
        aht.call(SOAP_ACTION, soapEnvelope);
        SoapPrimitive response = (SoapPrimitive)soapEnvelope.getResponse();

        tv.setText("REZULTAT:"+response);       
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

I made a webservice using Eclipse WTP and Axis2 it is a simple method which returns random numbers:

package de.vogella.webservice.soap.axis2;

import java.util.Random;

public class RandomNumber {
    public float[] getNumber(byte[] image){
        float u[] = new float[8];
        for(int i = 0; i < 8; i++) {
            u[i] = new Float(i);
            Random random = new Random();
            u[i]= random.nextFloat();
        }
        return u;
    }
}

All I need to do is to link this three and my work is done. Can someone please help?

Upvotes: 1

Views: 8761

Answers (2)

Kumar
Kumar

Reputation: 19

Instead of marshaling you can continue with your code:

String strBase64 = Base64.encode(imagebyte);
Request.addProperty("image", strBase64); 

In your webmethod decode the string as below:

byte[] data=Base64.decode(image);

Upvotes: 2

Ben Siver
Ben Siver

Reputation: 2948

It's not clear from your question where exactly you are storing the image. The webservice code you have shown just returns a number and does not support HTTP POST requests. It seems to me from your code that you need to adjust your webservice to accept this type of request and store your marshalled byte array in a database. So the next logical step would be to create a database for storing these images, and then updating your webservice to connect to this DB and insert the image data. Finally your Android app would call the image marshalling code and then make the webservice call with the resultant byte array.

Upvotes: 0

Related Questions