Image Upload Using Retrofit2 Put Method
I am new to retrofit2. I want upload image through api service as a file. I have tried with postman by selecting a file and found its working. But through mobile how can I upload t
Solution 1:
You have to create Multipart Request like below.
Filefile=newFile(filePath);
RequestBodyreqFile= RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Partbody= MultipartBody.Part.createFormData("picture", file.getName(), reqFile);
RequestBodyname= RequestBody.create(MediaType.parse("text/plain"), "picture");
RequestBodyname= RequestBody.create(MediaType.parse("text/plain"), "your_name");
RequestBodyemail= RequestBody.create(MediaType.parse("text/plain"), "your_email");
RequestBodyphone= RequestBody.create(MediaType.parse("text/plain"), "your_phone");
HasMap<String,RequestBody> map = newHasMap<>();
map.put("name",name);
map.put("email",email);
map.put("phone",phone);
postImage(map, picture)
Interface is like below.
@Multipart@PUT("/")
Call<ResponseBody> postImage(@PartMap Map<String, RequestBody> map, @Part MultipartBody.Part image);
}
Also, don't forget to add @Multipart in your interface.
Post a Comment for "Image Upload Using Retrofit2 Put Method"