Android how to get picture or video thumbnail _android

Source: Internet
Author: User

Gets the thumbnail based on the specified image path and size This method has two advantages:

1. Using a smaller memory space, the first bitmap is actually null, just to read the width and height, the second read bitmap is based on the proportional compression of the image, the third read bitmap is the desired thumbnail.

2. The thumbnail is not stretched for the original image, it uses the 2.2 version of the new tool thumbnailutils, the image generated using this tool will not be stretched.

/** * @param ImagePath * The path of the image * @param width * Specifies the width of the output image * @param height * Specifies the height of the output image * @return The resulting indent
 Sketch */public static Bitmap Getimagethumbnail (String imagepath, int width, int height) {Bitmap Bitmap = null;
 Bitmapfactory.options Options = new Bitmapfactory.options ();
 Options.injustdecodebounds = true;
 Get the width and height of this picture, note that the bitmap here is null bitmap = Bitmapfactory.decodefile (ImagePath, Options); Options.injustdecodebounds = false;
 Set to FALSE//compute 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 (is <= 0) {be = 1;
 } options.insamplesize = be; Reread the picture, read the scaled bitmap, and note that this time you want to set the Options.injustdecodebounds to False bitmap = Bitmapfactory.decodefile (ImagePath, Options
 );
  Use Thumbnailutils to create thumbnails, where you specify which bitmap object to scale bitmap = Thumbnailutils.extractthumbnail (bitmap, width, height, Thumbnailutils.optionS_recycle_input);
 return bitmap;
  /** * Gets the thumbnail of the video first by Thumbnailutils to create a thumbnail of the video, and then using thumbnailutils to generate a thumbnail of the specified size.
  * If the desired thumbnail width and height are less than micro_kind, the type uses 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 * Specifies the height of the output video thumbnail * @param k
  IND * 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 static Bitmap Getvideothumbnail (String
 Videopath, int width, int height, int kind) {Bitmap Bitmap = null;
 Gets the thumbnail image of the video bitmap = 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);
 return bitmap;
 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.