Upload Multiple Images to Server in Android

We've already shown you in a tutorial how to upload files to a server. In this tutorial, nosotros'll show you a special instance: uploading multiple files with one asking.

If you want to catch upwards with one of our plenty Retrofit topics, check our outline of posts:

Retrofit Series Overview

Upload Multiple Files With Retrofit 2

Earlier going into the details of uploading multiple files, please brand certain you sympathise the principles of uploading files with Retrofit 2. The arroyo for multiple files is going to exist very like.

First of all, we demand to extend our FileUploadService class for the new upload with multiple files:

          public interface FileUploadService {       // previous code for single file uploads     @Multipart     @Post("upload")     Call<ResponseBody> uploadFile(             @Part("description") RequestBody clarification,             @Role MultipartBody.Part file);      // new lawmaking for multiple files     @Multipart     @Post("upload")     Phone call<ResponseBody> uploadMultipleFiles(             @Part("description") RequestBody description,             @Role MultipartBody.Part file1,             @Role MultipartBody.Function file2); }                  

If you compare the ii interface methods for uploadFile() and uploadMultipleFiles(), you can spot the divergence very easily. Retrofit 2 makes it simple to add new file parts. On the side of the interface declaration, we just need to add another line @Role MultipartBody.Part file. The uploadMultipleFiles() method currently only supports exactly two files. You need to accommodate the corporeality of MultipartBody.Part parts to your needs. In a later tutorial we'll expect at a way to add together a dynamic amount of files to your request.

Declaring the interface is only half of the work. The other function is the implementation in the activity or fragment lifecycle. Since we're working with multiple files now, we've implemented two helper methods to make things more robust:

          @NonNull private RequestBody createPartFromString(Cord descriptionString) {       render RequestBody.create(             okhttp3.MultipartBody.Form, descriptionString); }  @NonNull individual MultipartBody.Role prepareFilePart(Cord partName, Uri fileUri) {       // https://github.com/iPaulPro/aFileChooser/hulk/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java     // use the FileUtils to get the actual file past uri     File file = FileUtils.getFile(this, fileUri);      // create RequestBody instance from file     RequestBody requestFile =         RequestBody.create(             MediaType.parse(getContentResolver().getType(fileUri)),              file         );      // MultipartBody.Part is used to send also the actual file name     return MultipartBody.Part.createFormData(partName, file.getName(), requestFile); }                  

The createPartFromString() method tin can be useful for sending descriptions forth a multipart upload. The prepareFilePart() method builds a MultipartBody.Part object, which contains a file (like a photo or video). The wrapping in a MultipartBody.Part is necessary to upload files with Retrofit two.

So let'southward use our helper methods to ship two files:

          Uri file1Uri = ... // go it from a file chooser or a camera intent   Uri file2Uri = ... // get it from a file chooser or a camera intent  // create upload service client FileUploadService service =           ServiceGenerator.createService(FileUploadService.grade);  // create part for file (photo, video, ...) MultipartBody.Part body1 = prepareFilePart("video", file1Uri);   MultipartBody.Part body2 = prepareFilePart("thumbnail", file2Uri);  // add together another part within the multipart asking RequestBody clarification = createPartFromString("hello, this is description speaking");  // finally, execute the request Phone call<ResponseBody> phone call = service.uploadMultipleFiles(description, body1, body2);   call.enqueue(new Callback<ResponseBody>() {       @Override     public void onResponse(Call<ResponseBody> call,             Response<ResponseBody> response) {         Log.v("Upload", "success");     }      @Override     public void onFailure(Call<ResponseBody> call, Throwable t) {         Log.eastward("Upload error:", t.getMessage());     } });                  

That'southward all you need to practise to send multiple files in one request. Of grade, you could add together some other or many more parts to the interface, if necessary. Using this approach you can send equally many files as you wish.

Summary

In this tutorial, yous've learned how to upload multiple files. Additionally, nosotros've shown y'all ii helper methods to make file handling easier for you. Retrofit 2 assists you by simplifying the process. If you run into any problems, allow usa know in the comments!


yalimaziname.blogspot.com

Source: https://futurestud.io/tutorials/retrofit-2-how-to-upload-multiple-files-to-server

0 Response to "Upload Multiple Images to Server in Android"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel