Get thumbnail images of videos and pictures under Android

Source: Internet
Author: User

Get video thumbnails: Thumbnailutils ways to get

/** * Get video thumbnails must be used in version 2.2 and above, because thumbnailutils is used in this class * to create a thumbnail of a video first through thumbnailutils, and then use Thumbnailutils to generate a specified size      Thumbnail image.      * 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 the path of the Videopath video * @param width Specifies the width of the output video thumbnail * @param height Specifies the height of the output video thumbnail * @param kind reference Mediasto Re.      Constants Mini_kind and Micro_kind in the Images.thumbnails class. * Where mini_kind:512 x 384,micro_kind:96 x * * @return A video thumbnail of the specified size */private Bitmap Getvideothu          Mbnail (String videopath, int width, int height, int kind) {Bitmap Bitmap = null;          Get thumbnail image of 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; }        }


Mediametadataretriever Way

Public Bitmap Getvideothumbnail (String filePath) {Bitmap Bitmap = null; Mediametadataretriever retriever = new Mediametadataretriever (); try {retriever.setdatasource (filePath); bitmap = Retriever.getframeattime ();} catch (IllegalArgumentException e) {e.printstacktrace ();} catch (RuntimeException e) {e.printstacktrace ();} finally { try {retriever.release ();} catch (RuntimeException e) {e.printstacktrace ();}} return bitmap;}


Content provider to get

public static Bitmap Getvideothumbnail (contentresolver cr, Uri uri) {Bitmap Bitmap = null; Bitmapfactory.options Options = new Bitmapfactory.options (); options.indither = False;options.inpreferredconfig = Bitmap.Config.ARGB_8888; cursor cursor = cr.query (uri,new string[] {mediastore.video.media._id}, NULL, NULL, NULL); if (cursor = = NULL | | Cursor.getcount () = = 0) {    return null;} Cursor.movetofirst (); String videoid = cursor.getstring (Cursor.getcolumnindex (mediastore.video.media._id));  Image ID in image table.sif (VideoID = = null) {return null;} Cursor.close (); Long Videoidlong = Long.parselong (videoid); bitmap = MediaStore.Video.Thumbnails.getThumbnail (CR, Videoidlong,images.thumbnails.micro_kind, options); return bitmap;}


Image thumbnail capture

* * * To 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 Bitmap Getimagethumbnail (String imagePath, int width, int height) {Bitmap Bitmap = null;          Bitmapfactory.options Options = new Bitmapfactory.options ();          Options.injustdecodebounds = true;          To 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//calculate zoom ratio int h = options.outheight;          int w = options.outwidth;          int bewidth = W/width;          int beheight = H/height;          int be = 1; if (Bewidth < beheight) {be = isWidth;          } 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 bitmap = Bitmapfactory.decodefile (ImagePath, opt          ions);                  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;     }




Get thumbnail images of videos and pictures under Android

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.