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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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);