简体   繁体   English

在Android Studio中使用离子库上传图片

[英]Image uploading using ion library in android studio

how to upload an image to server using API from android using ion library? 如何使用离子库从Android使用API​​将图像上传到服务器? I don't know much about ion library. 我对离子库了解不多。

As answered by @Ashwin Valento here you need to do this. 正如@Ashwin Valento回答的那样您需要执行此操作。

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. 或者,您可以像这里一样以base64格式发送图像 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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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