How to Upload a photo using Retrofit?

江枫思渺然 提交于 2019-11-29 15:18:58
raniejade

Try using @Body instead of @Part.

@PATCH("/api/users/{username}/")
Call<User> changeUserPhoto(@Header("Authorization") String token,@Path("username") String userName , @Body RequestBody photo);

then use MultipartBuilder to build the RequestBody

RequestBody photo = RequestBody.create(MediaType.parse("application/image"), file);
RequestBody body = new MultipartBuilder()
        .type(MultipartBuilder.FORM)
        .addFormDataPart("photo", file.getName(), photo)
        .build();

Call<User> call = userService.changeUserPhoto(token, username, body);
...

EDIT:

You can also check my answer to a very similar question.

The better option would be converting bitmap to string and upload as string.

Convert your photo to base64 and upload it as a simple string body.

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