/*** 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. * * @paramImagePath * Path to image *@paramWidth * Specifies the breadth of the output image *@paramHeight * Specifies the altitude of the output image *@returngenerated 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 thumbnail images of videos first create a thumbnail of a video with 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. * * @paramVideopath * Path to video *@paramWidth * Specifies the output video thumbnail size *@paramHeight * Specifies the altitude of the output video thumbnail *@paramKind * Refer to constants Mini_kind and Micro_kind in the MediaStore.Images.Thumbnails class. * of which, mini_kind:512 x 384,micro_kind:96 x *@returnSpecify the size of the video thumbnail*/ 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