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, we'll testify you lot a special case: uploading multiple files with ane asking.

If y'all want to catch upwardly with i of our plenty Retrofit topics, bank check our outline of posts:

Retrofit Series Overview

Upload Multiple Files With Retrofit ii

Before going into the details of uploading multiple files, please make sure you lot understand the principles of uploading files with Retrofit 2. The arroyo for multiple files is going to exist very similar.

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

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

If you lot compare the two 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 announcement, we only demand to add another line @Part MultipartBody.Part file. The uploadMultipleFiles() method currently only supports exactly two files. You demand to adjust the amount of MultipartBody.Part parts to your needs. In a afterwards tutorial we'll await at a way to add a dynamic corporeality of files to your request.

Declaring the interface is only one-half of the work. The other part is the implementation in the activity or fragment lifecycle. Since nosotros're working with multiple files at present, nosotros've implemented two helper methods to make things more than robust:

          @NonNull private RequestBody createPartFromString(Cord descriptionString) {       return RequestBody.create(             okhttp3.MultipartBody.Course, descriptionString); }  @NonNull private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {       // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java     // use the FileUtils to get the bodily 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 transport also the actual file name     return MultipartBody.Part.createFormData(partName, file.getName(), requestFile); }                  

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

And then let'southward utilise our helper methods to send two files:

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

That's all you need to do to send multiple files in one request. Of course, yous could add another or many more parts to the interface, if necessary. Using this approach you can send as many files as you lot wish.

Summary

In this tutorial, you lot've learned how to upload multiple files. Additionally, we've shown you two helper methods to make file handling easier for you lot. Retrofit 2 assists you by simplifying the process. If you run into any issues, let us know in the comments!


brownbestudy.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"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel