Android developmentAndroid tutorial

Multipart Image Upload in Android – Retrofit 2 android upload file

Multipart Image Upload in Android

Hi, Android developer in this article I share Multipart Image Upload in Android – Retrofit 2. Like in your project you can upload an image using multipart rest API. Multipart upload refers to creating an object using the data in the object and then uploading each part to HCP. Multipart upload results in a single object which behaves precisely the same way as objects that were stored using a single PUT request object request. A multipart object is an object that has been created using a multipart upload.

Multipart uploads make it easy to store large objects. Multipart uploads are great for large objects.

Multiple parts of an object can be uploaded to HCP simultaneously, which speeds up the storage time.

Before you upload data to HCP, you don’t have to know its full size. Multipart uploads allow streaming data to be stored in real-time.

It is possible to store an object for a long time. You can spread the uploads of individual parts across time to reduce bandwidth usage when high-bandwidth operations continue.

It is possible to avoid having to repeat large upload operations if the connection is lost during the upload. Each part that you upload may be small so the upload time can be quick.


Upload operations in multiple parts

These operations are necessary to create an object using a multipart uploaded:

1. Initiate multipart upload (see How to initiate a multipart Upload).

2. Upload object data parts You can upload a part using either one of these operations

Upload the part using data outside of HCP (see Uploading part of a multipart upload).

Upload the part by using data from another object in HCP ( Uploading part of a multipart Upload).

3. Complete the multipart Upload (see Completing a Multipart Upload).

These operations can also be performed for multipart uploads

Abort multipart upload (see Aborting multipart upload).

List the parts in an in-progress multipart Upload (see Listing of the Parts of a Multipart Upload).

List the multipart uploads currently in progress for each bucket (see Listing all in-progress multipart Uploads in a Bucket).

Introducing Amazon S3 Multipart Upload

Amazon S3 is pleased to announce Multipart Upload. This allows for faster and more flexible uploads. Multipart Upload allows for you to upload one object as a collection of parts. Once all the parts have been uploaded, Amazon S3 presents the data as one object. This feature allows you to create parallel uploads. You can pause, resume, or start uploads before you know how large your object is. Multipart Upload is described in the Amazon S3 Developer Guide.


Multipart Image Upload in Android – Retrofit 2 android upload file

Let’s Start to make an example for Multipart Image upload in android. Start your android studio and create a new project if you have an existing open project and follow the below steps. for multipart image upload in android.


Step 1:  – Need to add dependencies for the Retrofit and Retrofit GSON converter library.

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'

Step 2: –  Add Internet and Storage permission in our AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Step 3:-  User Below Method for multipart image upload in android

user this method for Upload image in multipart. Pass your capture image URI in this method and send it to the server.


On activity, results to check image capture on not when your result is Ok to call your API to upload the image to server .

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && requestCode == RequestCodeOne) {
        ArrayList<String> returnValue = data.getStringArrayListExtra(Pix.IMAGE_RESULTS);
        Uri imageUri = Uri.parse(new File(returnValue.get(0)).getAbsolutePath());
    } else {



Method to Upload Image to server.

public void UpdateProfielImage(String Image) {
    if (Utils.isNetworkAvailable(this)) {
        Call<CustomerFeedback> call;, this.getResources().getString(R.string.please_wait));
        MultipartBody.Part filePart = null;
        if (Image.equals("")){
            RequestBody attachmentEmpty = RequestBody.create(MediaType.parse("text/plain"), "");
            filePart = MultipartBody.Part.createFormData("file", "", attachmentEmpty);
        }else {
            final File file = new File(Image);
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            filePart = MultipartBody.Part.createFormData("file", file.getName(),
        call = MyApplication.apiServiceInterface.ProfileImage(filePart, Stash.getString(Constants.AUTH_TOKEN));
        call.enqueue(new Callback<CustomerFeedback>() {
            public void onResponse(Call<CustomerFeedback> call, Response<CustomerFeedback> response) {
                if (response.body() != null) {
                    CustomerFeedback createLeadResponse = response.body();
                    Log.e("Upload Image ", "->" + new Gson().toJson(createLeadResponse));
                    int statusCode = createLeadResponse.code;
                    if (!Utils.validateServiceResponse(statusCode, coordinatorLayout, activity)) {
                        //Password setup for new registration and password enter screen for login user's.
                        if (statusCode == Constants.RESPONSE_SUCCESS) {
                            Toast.makeText(activity,"Successfully Updated",Toast.LENGTH_LONG).show();

                        } else {
                            Toast.makeText(activity,"Something Went Wrong",Toast.LENGTH_LONG).show();


            public void onFailure(Call<CustomerFeedback> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
                        coordinatorLayout, Constants.SNACK_BAR_COLOR_ID, Constants.SNACK_BAR_DISPLAY_TIME);
    } else {
                coordinatorLayout, Constants.SNACK_BAR_COLOR_ID, Constants.SNACK_BAR_DISPLAY_TIME);

Step 4: –  Defined a Retrofit interface class with Multipart as the type to upload the image.

public interface ApiInterface {

// Customer Profile Image Update
   Call<CustomerFeedback> ProfileImage(@Part MultipartBody.Part file,
                                 @Header(Constants.AUTH_TOKEN_KEY) String tokenHeader);

Step 5: –Creating a Model class with message fields.

public class CustomerFeedback {
    public int code;
    public String result ; // For success.
    public ArrayList<ServiceErrorMessage> resultmsg;//If any server error occured
    public class Result {
        public String result;



Read More:-