upload image from android to webservice

帅比萌擦擦* 提交于 2019-12-07 06:02:39

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8019868/upload-image-from-android-to-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!