简体   繁体   中英

Image uploading using ion library in android studio

how to upload an image to server using API from android using ion library? I don't know much about ion library.

As answered by @Ashwin Valento here you need to do this.

ArrayList<Part> fileParts = new ArrayList<>();

for (int i = 0; i < salonPhotos.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);

Here the image is being sent as a part of multipart form data to the server. Or you could send the image in base64 format like here. It's pretty straight forward.

My original answer is here which explains how you can upload multiple images.

If your requirement is to upload a single image, then you can do as

Ion.with(getContext())
.load("POST", MY_POST_URL)
.setMultipartFile("image", "image/png", new File("/sdcard/some_image.png"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM