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