The example in this article describes how Android caches pictures based on SoftReference. Share to everyone for your reference, specific as follows:
SoftReference in Java is a soft reference to an object. If an object has a soft reference and the memory space is sufficient, the garbage collector does not reclaim it, and if the memory space is low, the memory of those objects is reclaimed. The object can be used by the program as long as the garbage collector does not recycle it. Soft references can be used to implement memory-sensitive caching. Using soft references can prevent memory leaks and enhance the robustness of your programs.
The SoftReference feature is that an instance of it holds soft references to a Java object that does not prevent garbage collection threads from reclaiming the Java object. That is, once SoftReference has saved a soft reference to a Java object, the Get () method provided by the SoftReference class returns a strong reference to the Java object before the garbage thread reclaims the Java object. Also, once the garbage thread reclaims the Java object, the Get () method returns null
Bitmap object that caches soft references with the map collection:
map<string, softreference<bitmap>> imagecache = new New hashmap<string, softreference<bitmap> > ();
Strongly referenced Bitmap object
Bitmap Bitmap = Bitmapfactory.decodestream (InputStream);
Soft reference Bitmap Object
softreference<bitmap> bitmapcache = new softreference<bitmap> (Bitmap);
Add the object to the map so that it caches
imagecache.put ("1", softrbitmap);
The Bitmap object that takes soft references from the cache
softreference<bitmap> bitmapcache_ = Imagecache.get ("1");
Remove the Bitmap object, if the Bitmap is recycled due to insufficient memory, will get empty
Bitmap Bitmap_ = Bitmapcache_.get ();
Note: If you need to load a large number of pictures from the Internet at this time, consider using the sdcard to create a temporary folder cache these pictures
For more information on Android-related content readers can view the site topics: "Android Cache Operation tips Summary", "Android development memory and caching skills Summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls
I hope this article will help you with the Android program.