Android – Upload Image File Using Retrofit 2.0

Android – Upload Image File Using Retrofit 2.0

The previous posts guided you GET request in Retrofit. May that will useful to enhance the app with Retrofit’s built-in functionality. Android – Upload Image File Using Retrofit 2.0, this post will show you how to upload files to a backend server using the second major release of Retrofit, namely Retrofit 2.

Upload File - Retrofit 2.0+

Upload File – Retrofit 2.0+

Using Retrofit 2, you need to use either OkHttp’s RequestBody or MultipartBody.Part classes and encapsulate your file into a request body. Let’s have a look at the interface definition for file uploads.

We use the MultipartBody.Part class that allows us to send the actual file name besides the binary file data with the request. You’ll see how to create the file object correctly within the following section.

Android Code:

PHP code:

Full Code:

Activity Code:

For Permission Checker (Marshmallow user):

Permission Checker:

I have uploaded whole demo on github: RetrofitImageUpload

File uploads are an essential feature within up-to-date apps and you can integrate this feature within your app using Retrofit. This article guided you through the necessary steps to upload a file from your Android device to your backend server.

What to expect within the next post on Retrofit? Next week we’ll all about how to get back logging within Retrofit 2. Stay tuned, it will be a good shot!

Thank you.

Page Views (6236)

9606 Total Views 2 Views Today
Support with Your Share:

About pratikbutani

Enthusiastic Android Application Developer, Speaker at Google Developer Group - Rajkot, All time Learner of new things, Googler, Eager to Help IT Peoples. Master of all, Jack of ONE.

Comment Your Suggestion or Appreciation:

Loading Facebook Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *