Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

upload image from android to webservice

How can I upload an image file to a webservice using SOAP?

I need it to be used with SoapObejct so the webservice can handle the input file in its context and give it a new filename.

How? Any code examples?

Yoav

like image 377
Yoav Avatar asked Nov 05 '11 11:11

Yoav


1 Answers

Convert your image file to a Base64 string and send your string with its name to web-service easily. Do you still need code sample?

Edit

public static String fileToBase64(String path) throws IOException {
    byte[] bytes = Utilities.fileToByteArray(path);
    return Base64.encodeBytes(bytes);
}

public static byte[] fileToByteArray(String path) throws IOException {
    File imagefile = new File(path);
    byte[] data = new byte[(int) imagefile.length()];
    FileInputStream fis = new FileInputStream(imagefile);
    fis.read(data);
    fis.close();
    return data;
}

public class MyImage  {
public String name;
public String content;
}

send your object to the webservice as a JSON string:

in your activity:

MyClass myClass = new MyClass();
myClass.name = "a.jpg";
myClass.content = fileToBase64("../../image.jpg");
sendMyObject(myClass);

private void sendMyObject(
        MyImage myImage ) throws Exception {
    // create json string from your object
    Gson gson = new Gson();
    String strJson = gson.toJson(myImage);
    //send your json string here
    //...

}

In your webservice convert your json string to a real object which is a replica of MyClass.

edit:

Also you can ignore Json and have a webserivce method with 2 parameters: MyWebserviceMethod(string filename, string content); pass Base64 string as second parameter.

like image 66
Bobs Avatar answered Sep 28 '22 05:09

Bobs