Example code for Android uploading multiple pictures (rxjava asynchronous distribution)

I have been learning rxjava for some time and have been thinking about how to use it and how to use it reasonably in the project. In many Android projects, there will be image upload. Next, I will introduce how to upload multiple images asynchronously with rxjava.

1、 Framework used

In addition, rxjava is very consistent with lambda expression, so lambda configuration is introduced. Java8 features need to be supported in gradle:

To initialize the configuration, you need to initialize the network request framework in oncreate of your own application. If you deny it, you will not be able to make a network request.

2、 Image compression and upload

Here, in order to demonstrate the usage and image upload, it is only a simulation request, so three arrays are manually created to cache the URL after image selection and processing.

Most image uploads are based on multiple URI addresses selected by photographing or gallery. If not compressed, the images are large. Generally, the pictures taken are hundreds of KB or several megabytes. Therefore, in order to save traffic and server load, compression is required. The compressed image size is only about tens of KB.

In order to optimize the code and rxjava used by these time-consuming operations for asynchronous processing, we need to create the writing method of rxjava:

The distributed colleagues will make asynchronous network requests, upload pictures to the server, and return the URL and picture address stored by the server:

In order to save time, you can also compress pictures when adding pictures. When uploading, only the network operation of uploading is performed

3、 Summary

Here is just a simple demonstration of the basic usage of rxjava. There is no doubt that it is powerful, but to use it well, we still need to learn it deeply. It also makes our code more concise. There is no end to learning. Thank the bosses for providing us with so many useful frameworks.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>