/** * Get thumbnails based on the specified image path and size This method has two advantages: 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 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, using the new 2.2 version of the tool thumbnailutils, the image generated using this tool will not be stretched. * * @param imagePath * Image Path * @param width * Specify the width of the output image * @param height * Specify the height of the output image * @return generated thumbnails*/ Public StaticBitmap Getimagethumbnail (String ImagePath,intWidthintheight) {Bitmap Bitmap=NULL; Bitmapfactory.options Options=Newbitmapfactory.options (); Options.injustdecodebounds=true; //get the width and height of this picture, note that the bitmap here is nullBitmap =bitmapfactory.decodefile (ImagePath, Options); Options.injustdecodebounds=false;//set to False//Calculating the zoom ratio inth =Options.outheight; intW =Options.outwidth; intBewidth = w/width; intBeheight = h/height; intbe =1; if(Bewidth <beheight) { be=Bewidth; } Else{ be=Beheight; } if(Be <=0) { be=1; } options.insamplesize=Be ; //re-read the picture, read the scaled bitmap, and note this time to set the Options.injustdecodebounds to FalseBitmap =bitmapfactory.decodefile (ImagePath, Options); //use Thumbnailutils to create thumbnails, where you want to specify which bitmap object to scaleBitmap =thumbnailutils.extractthumbnail (bitmap, width, height, thumbnailutils.options_recycle_input); returnbitmap; } /** * Get thumbnails of videos first create a thumbnail of the video by Thumbnailutils, and then use thumbnailutils to generate a thumbnail of the specified size. * If the desired thumbnail width and height are less than micro_kind, then the type will use Micro_kind as the KIND value, which saves memory. * * @param videopath * Video Path * @param width * Specifies the width of the output video thumbnail * @param height * Specify the height of the output video thumbnail * @param kind * Refer to constants Mini_kind and Micro_kind in the MediaStore.Images.Thumbnails class. * Where mini_kind:512 x 384,micro_kind:96 x * * @return A video thumbnail of the specified size*/ Public StaticBitmap Getvideothumbnail (String Videopath,intWidthintheight,intkind) {Bitmap Bitmap=NULL; //get thumbnail images of a videoBitmap =Thumbnailutils.createvideothumbnail (Videopath, kind); System. out. println ("W"+bitmap.getwidth ()); System. out. println ("h"+bitmap.getheight ()); Bitmap=thumbnailutils.extractthumbnail (bitmap, width, height, thumbnailutils.options_recycle_input); returnbitmap; }
Android get picture or video thumbnail