Examples of Android Imagecache

Source: Internet
Author: User

The cache of the picture is very important for Androd mobile phone, here I bring a imagecache instance for everyone, hope can help everyone learn Android cache.

Package com.hnxw.xxms.utils;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.lang.ref.SoftReference;
Import Java.security.MessageDigest;
Import Java.util.HashMap;

Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import android.os.Environment;
Import Android.support.v4.util.LruCache;

Import Com.android.volley.toolbox.ImageLoader.ImageCache;

public class Myimagecache implements Imagecache {

/**
* PYX LRU Buffer
*/
Private lrucache<string, bitmap> LruCache;

/**
* Soft Reference cache
*/
Private hashmap<string, softreference<bitmap>> Softcache;

/**
* Context is used to obtain the current application's memory card cache file directory, this must be set
*/
Private context context;

/**
* Create Picture Buffer instance Context This field must be set, because this variable is required to get the buffer directory
*/
Public myimagecache (Context CT) {
5MB Memory Size
LruCache = new lrucache<string, bitmap> (5 * 1024 * 1024);
Soft references
Softcache = new hashmap<string, softreference<bitmap>> ();
context = ct;
}

/**
* Loaded from the cache, if NULL is returned, it is automatically loaded from the network
*
* @param URL
* @return
*/
@Override
Public Bitmap getbitmap (String URL) {
BITMAP ret = null;
if (URL! = null) {

The first step is to get the picture from the LRU cache
Bitmap Bitmap = lrucache.get (URL);

if (bitmap! = null) {//Find data from LRUCache
ret = bitmap; Find the image and return directly
} else {
If not found in the LRUCache, it is necessary to check whether the soft reference data has

softreference<bitmap> reference = softcache.get (URL);
if (reference! = NULL) {
Bitmap = Reference.get ();
}

if (bitmap! = null) {//finds the actual data from a soft reference, then returns
ret = bitmap;
Lrucache.put (URL, ret); Update LRU Cache
} else {
LruCache and SoftReference, then check the SD card for presence

TODO Find from SD card

String state = Environment.getexternalstoragestate ();
if (State.equals (environment.media_mounted)) {
There is an SD card to read

File directory =
Environment.getexternalstoragedirectory ();

File Imagecachefolder = new file (directory,
"Volleytestimages");

All files stored in this directory are emptied by the Android system when the application is uninstalled
Only the program can access it, this directory, other programs cannot access
File Cachedir = Context.getexternalcachedir ();

File Imagecachefolder = new file (Cachedir, "images");

Boolean bOK = true;

if (!imagecachefolder.exists ()) {
bOK = imagecachefolder.mkdirs ();
}

if (bOK) {
try {
MessageDigest Digest = MessageDigest
. getinstance ("MD5");
Digest.update (Url.getbytes ());
byte[] bytes = Digest.digest ();
String hex = hex (bytes);

File TargetFile = new file (Imagecachefolder,
HEX);

        if (Targetfile.exists ()) {
          //TODO reads the file and generates bitmap return, and joins to LRUCache
          //and joins to SoftReference
         try {
           fileinputstream fin = new FileInputStream (
             targetfile);
          bitmap = bitmapfactory
             .decodestream (Fin);
          fin.close ();
          ret = bitmap;

Lrucache.put (URL, bitmap);
Softcache.put (URL,
New Softreference<bitmap> (
bitmap));

} catch (IOException IoE) {

}
}
} catch (Exception ex) {
Ex.printstacktrace ();
}
}
}

}

}
}
return ret;
}

/**
* @param URL
* @param bitmap
*/
@Override
public void Putbitmap (String url, Bitmap Bitmap) {
Lrucache.put (URL, bitmap);
Softcache.put (URL, new softreference<bitmap> (Bitmap));

TODO store bitmap in SD card file

All files stored in this directory are emptied by the Android system when the application is uninstalled
Only the program can access it, this directory, other programs cannot access
File Cachedir = Context.getexternalcachedir ();

File Imagecachefolder = new file (Cachedir, "images");

Boolean bOK = true;

if (!imagecachefolder.exists ()) {
bOK = imagecachefolder.mkdirs ();
}

if (bOK) {
try {
MessageDigest digest = messagedigest.getinstance ("MD5");
Digest.update (Url.getbytes ());
byte[] bytes = Digest.digest ();
String hex = hex (bytes);

File TargetFile = new file (Imagecachefolder, hex);

try {
if (!targetfile.exists ()) {
bOK = targetfile.createnewfile ();
}
} catch (IOException IoE) {
Ioe.printstacktrace ();
}

if (bOK) {
TODO Write file
try {

FileOutputStream fout = new FileOutputStream (targetfile);

Bitmap.compress (Bitmap.CompressFormat.PNG, fout);

Fout.close ();
} catch (IOException IoE) {

}
}
} catch (Exception ex) {
Ex.printstacktrace ();
}
}

}

 private static String hex (byte[] bytes) {
  string ret = null;
  if (bytes! = null) {
   stringbuilder sb = new StringBuilder ();
   for (Byte abyte:bytes) {
    int h = (abyte >> 4) & 0x0F;
    int L = (abyte & 0x0F);
    char ch = 0, cl = 0;
    if (H > 9) {
     ch = (char) (' A ' + h-10);
    } else {
     ch = (char) (H + ' 0 ');
    }
    if (L > 9) {
     cl = (char) (' A ' + l-10);
    } else {
     cl = (char) (L + ' 0 ');
    }

Sb.append (CH). Append (CL);
}
ret = sb.tostring ();
SB = null;
}
return ret;
}

}

Examples of Android Imagecache

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.