Picture cache----Level Three cache

Source: Internet
Author: User

First, the image settings in the adapter class

Imagecachetask Imagecache = new Imagecachetas (context);

Final String imgcover = list.get (position). Getcover ();
Vh.img_movie_item_listview.setTag (Imgcover);
Bitmap BM = imagecache.loadimage (Imgcover, New Imagecallche () {
@Override
public void Onimageload (String imageUrl, Bitmap image) {
ImageView ImageView = (ImageView) Parent
. Findviewwithtag (Imgcover);
if (ImageView! = NULL && image! = null) {
Imageview.setimagebitmap (image);
}
}
});
if (BM! = null) {
Vh.img_movie_item_listview.setImageBitmap (BM);
} else {
Vh.img_movie_item_listview.setImageResource (R.drawable.ic_launcher);
}

Second, define the class Imagecachetask:


Import java.io.IOException;
Import java.io.UnsupportedEncodingException;
Import java.lang.ref.SoftReference;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.util.HashMap;

Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import Android.widget.Toast;

public class Imagecachetask {

Private hashmap<string, softreference<bitmap>> Imagecache;
Private context context;

Public Imagecachetask (Context context) {
This.context = context;
This.imagecache = new hashmap<string, softreference<bitmap>> ();
}

Interface for callbacks
Public interface Imagecallche {
void Onimageload (String imageUrl, Bitmap image);
}

Call this method first to the cache to find a photo of the bitmap object exists, exists directly with (return back), does not exist to start the thread to the server fetch, and then put in the cache for the next use
The callback object is the object that is created at the time of invocation, and is passed over for a number of hours after the service has taken the photo and is able to take this picture to the ImageView he belongs to.
Public Bitmap LoadImage (final String Imgurl, final Imagecallche callback) {

Using pictures from the cache
if (Imagecache.containskey (Imgurl)) {
Bitmap img = imagecache.get (imgurl). get ();
if (img! = null) {
return img;
} else {
Imagecache.remove (Imgurl);
}
}

Final Handler Handler = new Handler () {
@Override
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case 1:
Callback.onimageload (Imgurl, (Bitmap) msg.obj);
Break
Case 2:
Toast.maketext (Context, "network exception", 0). Show ();
Break
}
}
};

Load pictures from the server and use
New Thread (New Runnable () {
@Override
public void Run () {
if (isnetavailable (context)) {
Request a picture from the server
Bitmap img = getimagefromurl (imgurl);
Put the picture into the cache
Imagecache.put (Imgurl, New Softreference<bitmap> (IMG));
Message msg = handler.obtainmessage (1, IMG);
Handler.sendmessage (msg);
} else {
Handler.sendemptymessage (2);
}
}
}). Start ();
return null;
}

Request a picture resource from the server
Public Bitmap Getimagefromurl (String imgurl) {
URL url;
HttpURLConnection conn = null;
try {
url = new URL (imgurl);
conn = (httpurlconnection) url.openconnection ();
Conn.setconnecttimeout (5 * 1000);
Conn.setreadtimeout (5 * 1000);
if (conn.getresponsecode () = = 200) {
Return Bitmapfactory.decodestream (Conn.getinputstream ());
} else {
LOG.D ("Bright", "Server Exception");
}
} catch (Malformedurlexception e) {
LOG.E ("Bright", "" "+ E.getmessage ());
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
LOG.E ("Bright", "" "+ E.getmessage ());
E.printstacktrace ();
} catch (IOException e) {
LOG.E ("Bright", "" "+ E.getmessage ());
E.printstacktrace ();
} finally {
IF (conn! = null) {
Conn.disconnect ();
conn = null;
}
}
return null;
}

Determine if the network is unobstructed
public Boolean isnetavailable (context context) {
Connectivitymanager connmgr = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);

Networkinfo netInfo = Connmgr.getactivenetworkinfo ();//Get network communication information
if (NetInfo = = NULL | |!netinfo.isavailable ()) {//Determine if the network is unblocked
return false;
}
return true;
}

Determine if the network is unobstructed
public Boolean isnetworkavailable (context context) {
Connectivitymanager cm = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);

if (cm = = null) {
} else {
Networkinfo[] info = Cm.getallnetworkinfo ();
if (info! = null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getstate () = = NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}

}

Picture cache----Level Three cache

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.