Android Image Upload with Retrofit 2

Here is how to upload image with Retrofit 2 library on android.
Below is the service source code.

Call<ImageResponse> iNatObservationPhoto(@Part("file\"; filename=\"my_image.jpg\" ") RequestBody image);

And here is how I use it.

File photo = new File(imagePathLocation);
RequestBody photoBody = RequestBody.create(MediaType.parse("image/jpg"), photo);

// Synchronous.
Response<ImageResponse> response = service.iNatObservationPhoto(photoBody).execute();

// Asynchronous.
service.iNatObservationPhoto(photoBody).enqueue(new Callback<ImageResponse>() {
    public void onResponse(Call<ImageResponse> call, Response<ImageResponse> response) {

    public void onFailure(Call<ImageResponse> call, Throwable t) {


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.