Android load Picture Optimizer (ii) LruCache Disklrucache

Source: Internet
Author: User
Tags md5 encryption

LruCache (Memory Cache)

Disklrucache (disk cache)

1. Create a Disklrucache object

Private Static Disklrucache Disklrucache = Disklrucache.open (Cachedir, appversion, Valuecount, disk_cache_size);

Cachedir Data Cache Directory

AppVersion the current software version number is typically 1

Valuecount A key can save how many files are generally 1

Disk_cache_size the cache address of the disk

2.cacheDir getting the cache directory

Private File Getdiskcachedir () {        = Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate ()) | | |                Environment.isexternalstorageremovable ()? Context.getexternalcachedir (). GetPath ()                : Context.getcachedir (). GetPath ();        LOG.I ("Getdiskcachedir", cachepath+file.separator+disk_cache_subdir);         return New File (cachepath+file.separator+disk_cache_subdir);     = Getdiskcachedir ();

3. Use MD5 encryption to avoid special characters and then act as the cached key and get Disklrucache.editor instances

String imageUrl = "Http://10.2.2.1/img.jpg";   = Hashkeyfordisk (IMAGEURL);   = Mdisklrucache.edit (key);

4. With an instance of Disklrucache.editor, call its Newoutputstream () method to create an output stream and pass it into Downloadurltostream () to download and write to the cache.

Note that the Newoutputstream () method receives an index parameter, because the previous specified at the time of setting the Valuecount is 1, so here Index 0 can be. After the write operation is done, we also need to call the commit () method to commit for the write to take effect, and the Abort () method to abort the write.

NewThread (NewRunnable () {@Override Public voidrun () {Try{String ImageUrl= "Http://10.2.2.1/img.jpg"; String Key=Hashkeyfordisk (IMAGEURL); Disklrucache.editor Editor=Mdisklrucache.edit (key); if(Editor! =NULL) {OutputStream OutputStream= Editor.newoutputstream (0); if(Downloadurltostream (IMAGEURL, OutputStream)) {editor.commit (); } Else{editor.abort ();          }} mdisklrucache.flush (); } Catch(IOException e) {e.printstacktrace ();  }}). Start (); 

Android load Picture Optimizer (ii) LruCache Disklrucache

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.