Android Development uses the Acache class to cache data for level two (memory + files)

Source: Internet
Author: User

Acache class Download Address: Http://pan.baidu.com/s/1gfI0A9X

1. Create Acache instance

Acache Acache;

Acache = Acache.get (This, "main");

2, Save the data (memory and file)

Acache.put (Key,value,time);

The top two need not repeat, key-value pairs, last time, incoming int type, in seconds, representing the time that the cache exists, beyond which the cached data is purged.

3, get the cached data

Use an example to describe the problem:

The code is as follows Copy Code

public class Mainactivity extends activity {
Private ImageView img;
Acache Acache;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Acache = Acache.get (This, "main");
img = (imageview) Findviewbyid (r.id.img);

if (Acache.getasbitmap ("pic")!= null) {
LOG.E ("Acache", "caching is in use!");
Img.setimagebitmap (Acache.getasbitmap ("pic"));
} else {
Picasso.with (this). Load (' http://www.b.hsgjxt.com/image/cache/data/0-100x100.jpg '). into (new Target () {
@Override
public void onbitmaploaded (Bitmap Bitmap, picasso.loadedfrom from) {
LOG.E ("Picasso", "Using picasso!");
Img.setimagebitmap (bitmap);
Acache.put ("Pic", bitmap, 10);
}

@Override
public void onbitmapfailed (drawable errordrawable) {

}

@Override
public void Onprepareload (drawable placeholderdrawable) {

}
});
}

}
}

When I first entered, the console printed the following:

When I immediately quit and went in, print as follows:

When I was delayed for about 10 seconds to go in again, and prompted as follows:

To prove that the cache is available, and that the cached time is also effective.

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.