Three solutions for Android loading a large number of image memory overflows
Method One: Only load thumbnails when loading images from the network or locally.
/** * Loading pictures by Path *
@param path of the path picture resource
* @param scalsize reduction in multiples *
@return */
public static Bitmap Loadresbitmap (String path, int scalsize)
{Bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = false; options.insamplesize = Scalsize; Bitmap bmp = Bitmapfactory.decodefile (path, options); return BMP; }
This method does not occupy a lot of memory, but its fatal disadvantage is that because the load is thumbnail, so the picture distortion is more serious, for the image quality requirements of high application, you can use the following method.
Method Two: The use of Java Soft reference, image caching, will often need to load the picture, stored in the cache, to avoid repeated loading.
For a detailed description of soft references (softreference),
/** * * @author Larson.liu * This class is used for picture caching to prevent memory overflow */
Public class Bitmapcache {static * Bitmapcache cache;/**
Storage for Chche content */* Hashtable bitmaprefs; /** Garbage Reference Queue (the referenced object has been reclaimed, the reference is put into a queue) */* Referencequeue q; /** * Inherits the SoftReference so that each instance has an identifiable identity. */* Class Btimapref extends SoftReference {* Integer _key = 0; public btimapref (Bitmap bmp, Referencequeue q, int Key) {super (BMP, q); _key = key;}} * Bitmapcache () {bitmaprefs = new Hashtable (); q = new Referencequeue (); } /** * Get cache instance */Public stat IC Bitmapcache getinstance () {if (cache = = null) {cache = new Bitmapcache ();} return cache; } /** * in soft reference Reference to an instance of a Bitmap object and save the reference * * void Addcachebitmap (Bitmap bmp, Integer key) {Cleancache ();//clear garbage reference btimapref ref = new Bt Imapref (BMP, Q, key); Bitmaprefs.put (key, ref); /** * Retrieves an instance of the corresponding Bitmap object based on the image resource ID number under the specified drawable (which can be obtained from the network or local path, depending on your needs). */Public Bitmap getbitmap (int resId, Context context) {Bitmap BMP = null;//The cache has a soft reference to the Bitmap instance, if any, obtained from a soft reference. if (Bitmaprefs.containskey (resId)) {Btimapref ref = (BTIMAPREF) bitmaprefs.get (resID); BMP = (Bitmap) ref.get (); }//If there is no soft reference, or if the instance obtained from the soft reference is NULL, rebuild an instance,//and save the soft reference to the new instance if (BMP = = null) {BMP = Bitmapfactory.decoderesource (context.g Etresources (), resId); This.addcachebitmap (BMP, ResId); } return BMP; } * void Cleancache () {btimapref ref = null; while (ref = (BTIMAPREF) q.poll ()) = null) {Bitmaprefs.remove (ref._ Key); }} //clears all content in the cache public void ClearCache () {Cleancache (); Bitmaprefs.clear (); System.GC (); System.runfinalization (); }  , calling the class in the program code:  IMAGEVIEW.SETIMAGEBITMAP (Bmpcache.getbitmap (R.DRAWABLE.KIND01, this)); So when your imageview needs to change the background picture back and forth, it doesn't need to be loaded again.
Method Three: Destroy the Bitmap object that is no longer used in time.
if (bitmap! = null && b!itmap.isrecycled ()) {bitmap.recycle (); bitmap = null; Recycle () is a lengthy process, set to NULL, and then at the end of the call System.GC (), the effect can be much better} system.gc ();
Three ways Android loads a lot of picture memory overflows