/** * Get thumbnails based on the specified image path and size * This method has two benefits: * 1. Using a smaller memory space, the first fetch of the bitmap is actually null, just to read the width and height, * The second read of the bitmap is based on the proportional compressed image, the third read bitmap is the desired thumbnail. * 2. Thumbnails are not stretched for the original image, and the 2.2 version of the new tool thumbnailutils is used, so that the images generated with this tool are not stretched. * @param the path of the ImagePath image * @param width specifies the output image widths * @param height Specifies the height of the output image * @return generated thumbnails */P rivate void Getimagethumbnail (ImageView thumbnails_img,string imagepath,int width,int height) {asy = new Asyncimaget Ask (Thumbnails_img,width,height); Asy.execute (ImagePath); /** * Use async line loads to load image * thread pool */private final class Asyncimagetask extends Asynctask<string,integer,bitma p>{private ImageView thumbnails_img; private int width; private int height; Public Asyncimagetask (ImageView thumbnails_img,int width, int height) {this.thumbnails_img = Thumbnails_img;this.width = Width;this.height = height;} Protected Bitmap doinbackground (String ... params) {img_bitmap = null;Save Memory Options.inpreferredconfig = bitmap.config.argb_4444;/* setting allows decoder to decode in the best way */options.inpurgeable = True;o ptions.ininputshareable = True;options.injustdecodebounds = true; If Dither is true, the decoder would attempt to dither the decoded imageoptions.indither = false;//do not take picture jitter processing//Get the width and height of this picture , note that the bitmap here is null Img_bitmap = Bitmapfactory.decodefile (params[0], options); options.injustdecodebounds = false;//is set to False//calculates the scaling ratio int h = options.outheight; int w = options.outwidth; int bewidth = W/width; int beheight = H/height; int be = 1; if (Bewidth < beheight) {be = Bewidth; } else {be = Beheight; } if (be <= 0) {is = 1; }options.insamplesize = be; Re-read into the picture, read the scaled bitmap, note this time to set Options.injustdecodebounds to False img_bitmap = Bitmapfactory.decodefile (Params[0], Options); Use Thumbnailutils to create thumbnails, where you specify which bitmap object to scale Img_bitmap = Thumbnailutils.extractthumbnail (img_bitmap, width, height, Thumbnailutils.options_recycle_input); return img_bitmap;} Processing result protected voidOnPostExecute (Bitmap result) {if (result!=null&&thumbnails_img! = null) {Thumbnails_img.setimagebitmap ( result); Asy_list.add (asy); asy = Null;//img_bitmap.recycle (); Img_bitmap=null;}} }
Android Asynchronous load image (thumbnail display) implementation