Picture level Three caching process

Source: Internet
Author: User

Picture level Three caching process
Content of Level three cache:
1. Get pictures from memory, have, load display
2. If not in memory, get the picture from the local, have the load display, and cache the picture to memory, prepare for the next display
3. If there is no local, download pictures from the network, download complete, display pictures, cache to memory, save to local file, prepare for the next display
There are two ways of getting pictures in memory
The first: the way of soft references (less commonly used)
Strong reference: User = new UserInfo (), will not be easily reclaimed by the system
Soft reference: Softreference<bitmap> When the memory is low, the system recycles the soft reference
Weak references: Weakreference<bitmap> When memory is low, the system reclaims weak references, and if both soft and weak references are present, the weak references are recycled first
Virtual reference: PHANTOMREFERENCE&LT;BITMAP&GT; When memory is low, the system reclaims weak references, lower precedence than weak references
The second kind: LRUCache Way
Determine the number of times a picture has been referenced in the latest period of time, determine if a cache is required, and cache the music with high frequency to memory
1. Create a LRUCache Object
Public Mycachebitmaputils () {
Map = new hashmap<string, softreference<bitmap>> ();
MaxSize: Cache space size, typically one of 8 of total memory
int maxSize = (int) (Runtime.getruntime (). TotalMemory ()/8);
LruCache = new lrucache<string, bitmap> (maxSize) {
Get the size of a cached picture
@Override
protected int sizeOf (String key, Bitmap value) {
Value.getrowbytes (): Gets the number of bytes occupied by a picture line
return Value.getrowbytes () * Value.getheight ();
}
};
}
2. Cache pictures
public void Savebitmap (String url,bitmap Bitmap) {
softreference<bitmap> softreference = new softreference<bitmap> (BITMAP);//modify Bitmap references with soft references
Map.put (URL, softreference);
Lrucache.put (URL, bitmap);
}
3. Get Cached pictures
Public Bitmap getbitmap (String URL) {
/*softreference<bitmap> softreference = map.get (URL);
Verify that soft references are not recycled
if (softreference! = null) {
Bitmap Bitmap = Softreference.get ();
return bitmap;
}*/
Bitmap Bitmap = lrucache.get (URL);
return bitmap;
}
4, put the above three steps in a tool class called by the tool class
Local cache
Saving and reading files via IO stream operation
1. Save the picture
File File = new file (dr, Md5util.md5 (URL). substring (0, 10));
FileOutputStream stream = new FileOutputStream (file);
Set picture type quality, save picture in local file
Parameter 1: Picture format
Parameter 2: Quality of the picture
Parameter 3: Write stream
Bitmap.compress (Compressformat.jpeg, N, Stream);
2. Get Pictures
File File = new file (PATH, md5util.md5 (URL). substring (0, 10));
Bitmap Bitmap = Bitmapfactory.decodefile (File.getabsolutepath ());
return bitmap;
Network access
URL murl = new URL (URL);
HttpURLConnection con = (httpurlconnection) murl.openconnection ();
Con.setconnecttimeout (5000);//Set link time-out
Con.setreadtimeout (5000);//Set Read timeout time
Con.connect ();//Link Network operation
int code = Con.getresponsecode ();//Get Server response code
if (code = = 200) {
Gets the server data, which is returned as a stream
InputStream stream = Con.getinputstream ();
Bitmap Bitmap = Bitmapfactory.decodestream (stream);
return bitmap;
}
Network operation is performed here using HttpURLConnection

Picture level Three caching process

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.