Android recursive compression upload multiple pictures to seven cattle example code

Recently, I encountered such a demand: to do a function of imitating wechat circle of friends, it is required to upload up to nine pictures to seven cows. Qiniu has an interface to upload pictures, but you can only upload one at a time. If you upload nine pictures together, you can't use the for loop. It's easy to make mistakes. Because the action of uploading seven cattle is completed in the sub thread, and the for loop is in the main thread, which is equivalent to starting ten sub threads to upload pictures at the same time, and they are time-consuming threads, which are easy to ANR.

The best solution should be to compress the picture between 200K and 300K to ensure that the picture is not distorted, and then upload it one by one. Through the callback of qiniu, judge whether the upload of the previous picture is completed. After the upload is completed, upload the next picture.

Well, don't say much, post code:

1. Compressed picture

This was compressed to 100k before. This is not compressed in proportion. Some pictures with large size will be blurred, so now it is adjusted to 250K. It is estimated that the compressed picture size will be about 200-300k.

2. Recursively upload pictures

This code is not difficult to understand. The details are written in the notes. Some are used in the project. You just need to know where to call the method for recursion, where to add counters, and where to end recursion.

However, this method has a small flaw. Normally, we have to have a loading progress bar in the process of uploading pictures. I found that the loading progress bar will get stuck slightly every time the upload is successful. It is preliminarily inferred that it is caused by compressing the picture. Compressing the picture is also energy-consuming, and it will cause jamming in the main thread. The proposed solution is: put the image compression part out in the sub thread, compress the image first, and then upload it after agreeing. But it hasn't come yet. Of course, if you have any good ideas, please put them forward. be deeply grateful.

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
分享
二维码
< <上一篇
下一篇>>