Imageview display optimization, secondary cache, and soft reference ., Imageview second-level cache

Source: Internet
Author: User

Imageview display optimization, secondary cache, and soft reference ., Imageview second-level cache

Package com. app. util; import java. io. file; import java. io. fileOutputStream; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import java. util. hashMap; import java. util. map; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmap. compressFormat; import android. graphics. bitmapFactory; import android. OS. asyncTask; import android. widget. image View;/*** asynchronously load the image and display the image (Bitmap) cache: 1. cache the bitmap object corresponding to the image in the memory (level-1 cache) 2. save the image to the mobile phone (second-level cache) 3. server Side */public class ImageLoader2 {private int defaultBitmapId; private Context context; // private Map of the set of cached image objects in memory <String, Bitmap> cache = new HashMap <String, bitmap> (); public ImageLoader2 (Context context, int defaultBitmapId) {this. defaultBitmapId = defaultBitmapId; this. context = context;}/*** load the Image Based on the image path and display the image * @ param Imagepath * @ param imageView */public void loadImage (String imagepath, ImageView imageView) {// Save the imageView tag. setTag (imagepath); // obtain the bitmap object Bitmap bitmap = getFromFirstCache (imagepath) from the first-level cache; if (bitmap! = Null) {// If yes, the imageView is displayed directly. setImageBitmap (bitmap); return;} // obtain the bitmap object bitmap = getFromSecondCache (imagepath) from the second-level cache; if (bitmap! = Null) {// If yes, the imageView is displayed directly. setImageBitmap (bitmap); return;} // obtain from the server // display the default image imageView. setImageResource (R. drawable. shop_photo_frame); loadImageFromNet (imagepath, imageView);}/*** get an image from the server, and displays * @ param imagepath * @ param imageView */private void loadImageFromNet (final String imagepath, final ImageView imageView) {new AsyncTask <Void, Void, Bitmap> () {@ Overrideprotected Bitmap doInBackground (Void ... Params) {String newPath = (String) imageView. getTag (); if (newPath! = Imagepath) // if they are different, the imageView has been reused. You do not need to load the server-side image return null; Bitmap bitmap = null; try {URL url = new URL (imagepath ); httpURLConnection connection = (HttpURLConnection) url. openConnection (); connection. setDoInput (true); connection. setConnectTimeout (5000); connection. setReadTimeout (5000); connection. connect (); if (connection. getResponseCode () == 200) {InputStream is = connection. getInputStream (); // bitmap of the image stream = BitmapFactory. decodeStream (is); if (bitmap! = Null) {// cache to a level-1 cache. put (imagepath, bitmap); // cached to the second-level cache. Save bitmap as an image String filename = imagepath. substring (imagepath. lastIndexOf ("/") + 1); File cacheFile = context. getCacheDir (); bitmap. compress (CompressFormat. JPEG, 100, new FileOutputStream (new File (cacheFile, filename) ;}} catch (Exception e) {e. printStackTrace ();} return bitmap;} @ Overrideprotected void onPostExecute (Bitmap bitmap) {String newPa Th = (String) imageView. getTag (); if (bitmap! = Null & newPath = imagepath) {// imageview.setimagebitmap(bitmap;}}}cmd.exe cute ();}/*** obtain the bitmap object from the second-level cache * the image is cached: /data/packagename/cache/* @ param imagepath: http ://... /f1.jpg * @ return */private Bitmap getFromSecondCache (String imagepath) {// obtain the image's local path String filename = imagepath. substring (imagepath. lastIndexOf ("/") + 1); File cacheFile = context. getCacheDir (); String filePath = cacheFile. getAbsolutePath () + "/" + filename; // data/packagename/cache/f1.jpg // load the image file to obtain the bitmap object Bitmap bitmap = BitmapFactory. decodeFile (filePath); return bitmap;}/*** obtain the bitmap object from the first-level cache * @ param imagepath * @ return */private Bitmap getFromFirstCache (String imagepath) {Bitmap bitmap = cache. get (imagepath); return bitmap ;}}

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.