Android development case and android case

Source: Internet
Author: User

Android development case and android case

This article does not cover the UI. If you want to know about the UI, skip this article.

This article will introduce in detail how to smoothly attach a local image library. For example, many apps are used at ordinary times, such as (SEE) and weibo. The image library functions are implemented. The main functions include:

  • All images are displayed by default.
  • Display images by directory

In addition, the interface elements include:

  • Thumbnail
  • Image directory list and number of images contained in the directory

Discussion: on Android, how does one smoothly load local photo albums?

 

Knowledge points

  • ContentProvider-Data access interface
  • CursorLoader-Cursor asynchronous Loader
  • Android Universal Image Loader

P.s. The difficulty in implementing the image library lies in how to quickly query images and directory information. It seems that Android does not directly provide such an interface.Android. provider. MediaStore. Images. MediaAndAndroid. provider. MediaStore. Images. Thumbnails. Although we can useThumbnailsThe thumbnail information and image ID are queried, but the image details are not provided. In addition, if the information or directory used to save the thumbnail is deleted (accidentally or manually), useThumbnailsBasically, it makes no sense. Therefore, we useMediaTo query images and directories.

 

Implementation Code

> Definition

static final Uri CONTENT_URI = Media.EXTERNAL_CONTENT_URI;static final String SORT_ORDER = Media.DATE_MODIFIED + " DESC";

Here, we only query the images on the sdcard and sort them in reverse order according to the modification time.

 

> Query Images

Code omitted and used directlyCursorLoaderPressSORT_ORDERSequentially load images. If a directory is specified, SetMedia. BUCKET_ID + "=? "Query conditions

 

> Query Directories

Like querying images, CursorLoader is also used to load data, but the parameters set are different, as shown below:

// Method 1 static final String [] PROJECTION_D = {"DISTINCT" + Media. BUCKET_ID, Media. BUCKET_DISPLAY_NAME, "COUNT (*) AS" + Media. _ COUNT}; // method 2 static final String [] PROJECTION = {Media. BUCKET_ID, Media. BUCKET_DISPLAY_NAME, Media. _ ID, "COUNT (*) AS" + Media. _ COUNT}; static final String SELECTION = "1 = 1) group by (" + Media. BUCKET_ID; static final String [] SELECTION_ARGS = null;

If the corresponding directory does not need to display the thumbnail of the first image, you can use method 1. Otherwise, use method 2 (PROJECTIONToSELETION). Set other parameters to CursorLoader.

 

Optimization Strategy

Finally, we need to mention that we use UIL (that is, Android Universal Image Loader) to load images, but each load is the source Image we query, according to the Global initial settings of the Demo code on github, the images on the first screen are no longer in the memory cache when they are loaded to the screen. If you want to display the images on the first screen again, you have to read the source image, and the user experience is poor. we can see that apps such as Android album and album are loaded smoothly. by observing these application caches, you can see that as long as you browse many unbrowsed images, the cache will become larger. therefore, you can think of the following methods:

  • Use UIL to cache thumbnail images of smaller sizes, such as 320px or 96px.

 

By now, the performance and user experience of the image library implemented in this method are almost the same as those of Android album. the only disadvantage of this method is that the app needs to cache thumbnails by itself. It can be assumed that if 80% of apps installed on the mobile phone have such a function page, isn't it necessary for each application to generate a set of thumbnails by itself?

 

END.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.