Search code examples
androidandroid-jsonion-koush

Upload image with Ion android library


I've got an app that is connected to a server through a restful API, but I need to upload an image to the server and I'm using Ion library, is there anyway to upload this image to the server?


Solution

  • You can use .setMultipartParameter("key","value") to upload image along with other text values as well..

    If you need to upload many images, You can use the "Part" class to add multiple images.

    ArrayList<Part> fileParts = new ArrayList<>();
    
    for (int i = 0; i < myImages.size(); i++) {
        Part part = new FilePart("image_name[" + i + "]",image_value[i]);
        fileParts.add(part);
    }
    
    
    Ion.with(getContext())
    .load("POST", MY_POST_URL)
    .setMultipartParameter("my_text_key", "my_text_value")
    .setMultipartParameter("my_text_key_2", "my_text_value_2")
    .addMultipartParts(fileParts);