Android. media. ThumbnailUtils

Source: Internet
Author: User

Android. media. ThumbnailUtils
A new class is added from API 8: android. media. the ThumbnailUtils class provides three static methods: one to obtain the Bitmap obtained from the first frame of the video, and the other two to scale down the image. Public static Bitmap createVideoThumbnail (String filePath, int kind) the first parameter is the video file path, the second parameter is to specify the image size, there are two options: Thumbnails. MINI_KIND and Thumbnails. MICRO_KIND. In the first document, the size is 512x384. I tested 544x960 using an MP4 file and 160x120 using a wmv file. Obviously unreliable. In the second format, the file size is 96x96, which is the thumbnail. ExtractThumbnail (Bitmap source, int width, int height, int options) extractThumbnail (Bitmap source, int width, int height) are used to process the Bitmap size, the first parameter is the Bitmap to be processed, the second parameter is the processed width, the third parameter is the height, and the fourth parameter is options. If options is defined as OPTIONS_RECYCLE_INPUT, the resource is recycled. That is to say, you can use the third method to convert the Bitmap of the first frame of the captured video to any desired size. The third method can also obtain the thumbnail of the image in the memory card. Bitmap bitmap = ThumbnailUtils. createVideoThumbnail (path1, Thumbnails. MINI_KIND); bitmap = ThumbnailUtils. extractThumbnail (bitmap, 210,210); a new type of MediaMetadataRetriever is added from API 10 to obtain information about a media file. Copy the code MediaMetadataRetriever mmr = new MediaMetadataRetriever. setDataSource ("/sdcard/33.mp4"); Bitmap bitmap = mmr. getFrameAtTime (); image. setImageBitmap (bitmap); System. out. println (mmr. extractMetadata (Med IaMetadataRetriever. METADATA_KEY_DATE) + ""); System. out. println (mmr. extractMetadata (MediaMetadataRetriever. METADATA_KEY_MIMETYPE) + ""); mmr. release (); MediaMetadataRetriever can be used to obtain thumbnails of any frame of a video. Public static Bitmap createVideoThumbnail (String filePath) {// MediaMetadataRetriever is available on API Level 8 // but is hidden until API Level 10 Class <?> Clazz = null; Object instance = null; try {clazz = Class. forName ("android. media. mediaMetadataRetriever "); instance = clazz. newInstance (); Method method = clazz. getMethod ("setDataSource", String. class); method. invoke (instance, filePath); // The method name changes between API Level 9 and 10. if (Build. VERSION. SDK_INT <= 9) {return (Bitmap) clazz. getMethod ("captureFrame "). invoke (instance);} else {Byte [] data = (byte []) clazz. getMethod ("getEmbeddedPicture"). invoke (instance); if (data! = Null) {Bitmap bitmap = BitmapFactory. decodeByteArray (data, 0, data. length); if (bitmap! = Null) return bitmap;} return (Bitmap) clazz. getMethod ("getFrameAtTime "). invoke (instance) ;}} catch (IllegalArgumentException ex) {// Assume this is a temporary upt video file} catch (RuntimeException ex) {// Assume this is a temporary upt video file .} catch (InstantiationException e) {Log. e (TAG, "createVideoThumbnail", e);} catch (InvocationTargetException e) {Log. e (TAG, "createVideoThumbnail", e );} Catch (ClassNotFoundException e) {Log. e (TAG, "createVideoThumbnail", e);} catch (NoSuchMethodException e) {Log. e (TAG, "createVideoThumbnail", e);} catch (IllegalAccessException e) {Log. e (TAG, "createVideoThumbnail", e) ;}finally {try {if (instance! = Null) {clazz. getMethod ("release"). invoke (instance) ;}} catch (Exception ignored) {}} return null ;}

Related Article

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.