Android App Image caching solution

Source: Internet
Author: User

This article provides three caching strategies: (1) LRU algorithm, fixed cache picture number (max_num), when the number of pictures exceeds max_num, delete the least recently used picture in the cache. (2) FTU algorithm, fixed each picture of the cache time limit, with the last use, the time limit after the deletion. (3) FMU algorithm, in memory fixed a certain size of storage space, more than the fixed space after the maximum size of the cache to delete the picture. You only need to pass the URL of the picture to the body of the method when you use it.

How to use:

1. Import jar;

2. access to services;

3. Submit the URL, to the program to determine whether to download.

The code is as follows Copy Code

public class Imagecachetacticsdemoactivity extends activity {
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.item);

/*fmu*/
Imagecachemanager = Imagecachemanager.getimagecacheservice (This,
imagecachemanager.mode_fixed_memory_used, "MEMORY");
Imagecachemanager.setmax_memory (1024 * 1024);

/*ftu*/
Imagecachemanager = Imagecachemanager.getimagecacheservice (This,
imagecachemanager.mode_fixed_timed_used, "time");
Imagecachemanager.setdelay_millisecond (3 * 60 * 1000);

/*lru*/
Imagecachemanager = Imagecachemanager.getimagecacheservice (This,
imagecachemanager.mode_least_recently_used, "num");
Imagecachemanager.setmax_num (5);

Imagecachemanager = Imagecachemanager.getimagecacheservice (This,
imagecachemanager.mode_no_cache_used, "NoCache");

Mimageview = (ImageView) Findviewbyid (R.id.imageview);
New Downloadtask ()
. Execute ("yun_qi_img/");
}

Private class Downloadtask extends Asynctask<string, Void, bitmap> {

@Override
Protected Bitmap doinbackground (String ... params) {
try {
Return Imagecachemanager.downlaodimage (New URL (params[0));
catch (IOException e) {
E.printstacktrace ();
}
return null;
}

@Override
protected void OnPostExecute (Bitmap result) {

Mimageview.setimagebitmap (result);
Super.onpostexecute (result);
}

@Override
protected void OnPreExecute () {
Mimageview.setimageresource (R.drawable.ic_launcher);
Super.onpreexecute ();
}

}

Private ImageView Mimageview;
Private Imagecachemanager Imagecachemanager;

}

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.