Android optimized query loads a large number of local album pictures

1、 Overview

Before explaining the optimization of photo album image query, let's take a look at the requirements proposed by PM. The requirements of PM are very simple, that is, to make a local photo album image query control similar to wechat, which mainly includes two parts:

These two requirements seem simple, but they actually hide a series of performance optimization problems. Before optimization, we investigated the performance of some other well-known apps in loading a large number of pictures (GIF recording is not clear enough, but the display problem is enough):

Several common software are tested below

Wechat: the picture query speed of wechat is still very fast. Basically, when you enter the picture selection page, the album data has been found, including the number of pictures in each picture directory and the URL of the cover picture. This experience is still quite good.

Sina Weibo: compared with wechat, the experience of sina Weibo is relatively poor. After entering the picture selection page, there is a black screen and then a white screen. There is not even a progress bar, which makes users think the app is dead. It will be displayed after a period of time. This experience is poor

QQ: the last 100 photos loaded on QQ are very fast, but after entering the camera album (there are more than 5000 photos), there is a progress bar waiting. After I experienced it, the waiting time is still relatively long. This experience is slightly better than Sina Weibo and worse than wechat

Idle fish: idle fish is the worst one. It is stuck for four or five seconds, then the black screen for two or three seconds, and finally displayed

2、 Comprehensive comparison

After comprehensive comparison, wechat is doing well. Basically, all photos can be displayed on the album page, and the album directory can be displayed very quickly!!!

After our investigation, we found that wechat adopts the circular paging loading strategy, and our optimization idea also adopts this strategy. First look at the optimized rendering:

When you enter the picture selection page, the pictures can be displayed very quickly. When you enter the photo album replacement page, the picture directory can also be displayed very quickly. There is no cache of the picture directory like wechat: first, the query speed is very fast, which is basically loaded in less than 2 seconds. Second, the latest data of the photo album can be refreshed in real time

Frequently switch various album directories, pictures can be queried very quickly, and the experience is still good!!!

3、 Optimized implementation

Optimize query album directory

Because there is no other good way to list all the photo album directory lists, you can directly request the query method of contentresolver to query. In order to speed up the query, some time-consuming judgments in the while loop are removed, and some logic to detect whether the picture is judged is moved out, which can be judged when it is used

URI of query picture

Because we only query the image URL and the directory where the image is located

PM requires photo albums to be arranged in chronological flashback. The creation and modification of pictures will affect the sorting of their directories. The sorting is arranged in chronological flashback

According to these query conditions, a cursor is obtained after query. The cursor contains all the image information we need. Then we traverse the cursor in a while loop. There must be no time-consuming operations in the while loop

In this way, you can quickly query all the picture directories, the number of pictures in the directory and the URL of the cover image. Here are three main optimization points:

Remove time-consuming judgment in while loop

In the previous code, there is a code to judge whether the file image exists:

It's terrible to put this code into the while loop. I tested it. If more than 5000 pictures need to be detected, the total time will increase by three or four seconds. This judgment can be put outside, and then make specific business judgment when operating which picture!

Prevent a picture folder from being scanned multiple times

A variable is added here to store the scanned image directory. The scanned images will not be processed:

After this optimization, the effect is still obvious. The same directory will not be scanned many times!

Get the number of pictures in the picture directory

This file Inside the list () method is a native method, which is very efficient!!!

Of course, the number of pictures in a directory can also be obtained by cursor query!!!

Query all photos in an album directory

Before introducing the photos in the query directory, let's first introduce two strategies for querying pictures. One is the one with more pictures in the directory, which can easily reach tens of thousands of pictures; The other is that there are few pictures in the directory, just hundreds of pictures

Load once policy

When the number of pictures in the directory is less than 1000, file The list native method loads all images at once. The native query efficiency is very fast. Thousands of images are queried at the second level

Cyclic paging loading policy

When the number of pictures is greater than or equal to 1000, the cyclic paging loading strategy is adopted. This strategy is specifically aimed at the situation with a large number of pictures. First, the pictures on the first page are loaded through paging, so that users can see the latest pictures at first sight, and then the background asynchronously queries the pictures on the next page until all pictures are queried, This is also wechat's photo album query strategy.

Implementation of one-time loading strategy

Let's look at the policy implementation code next time. First, filter out the files suffixed with image format through the list method of file and return an image path array

Because we want an array arranged by time flashback, we need to sort the array queried above. Here we use the file LastModified method

Implementation of cyclic paging loading strategy

This strategy draws lessons from wechat and loads pictures page by page until all pictures are loaded.

The core here is the query criteria, which adds a directory you want to query to the query parameters

This selection must not be misspelled, otherwise it cannot be queried

Because of paging, sortorder is not simply arranged according to the time flashback

Finally, loop through cursor to get the image path we want

PAGE_ Size is a constant, which indicates how many pieces of data we want to query at a time. We set 200 here. We query 200 pieces of data at a time. PageIndex is the page on which to query, starting from 0

At the beginning, query the data of the first page. When the size of the queried data list is greater than or equal to the PageSize we want to query, we think there is a next page. PageIndex plus 1 circularly queries the next page until the size of the queried list is less than PageSize.

After the above steps of optimization, there is basically no problem loading local album pictures. After real machine testing, 5549 pictures can be queried very quickly, which is comparable to wechat and gallery.

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