Android-Get video first frame thumbnail

Source: Internet
Author: User

Dry

Starting with API 8 , a new class has been added:

Android.media.ThumbnailUtils This class provides 3 static methods for getting the first frame of the video to get the bitmap,2 of the thumbnail processing of the image.

 Public Static int kind)

The first parameter is the path to the video file, the second parameter is the size of the specified picture, and there are two options thumbnails.mini_kind and thumbnails.micro_kind.

    • The first document says size is x 384, I test it with a MP4 format file to get 544 x 960, and test it with a WMV format file for x 120. Obviously not reliable.
    • The second parameter is the size of the two format files is 96, this is the thumbnail.
int int int  intint height)

Both methods are used to handle the size of the bitmap, the first parameter is the bitmap to be processed, the second parameter is the processing width, the third is the height, the fourth parameter options, and if the options are defined as Options_recycle_input, the resource is recycled. In other words, you can use the third method to bitmap the first frame of the captured video into any desired size, and the third method can also get a thumbnail image of the memory card.

Bitmap Bitmap = thumbnailutils.createvideothumbnail (path1, thumbnails.mini_kind);   = Thumbnailutils.extractthumbnail (bitmap, 210, 210);

A new class of mediametadataretriever can be used to get media files from API

New mediametadataretriever ();  Mmr.setdatasource ("/sdcard/33.mp4");   = mmr.getframeattime ();  Image.setimagebitmap (bitmap);  System.out.println (Mmr.extractmetadata (mediametadataretriever.metadata_key_date)+ "");  System.out.println (Mmr.extractmetadata (mediametadataretriever.metadata_key_mimetype)+ "");  Mmr.release ();

Mediametadataretriever can get thumbnails of any frame in the video.

 Public StaticBitmap createvideothumbnail (String filePath) {//Mediametadataretriever is available on API level 8//But was hidden until API levelClass<?> 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)returnbitmap; }              return(Bitmap) Clazz.getmethod ("Getframeattime")). Invoke (instance); }      } Catch(IllegalArgumentException ex) {//assume this is a corrupt video file}Catch(RuntimeException ex) {//assume this is a corrupt 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; }
I'm the dividing line of the king of the Land Tiger.

Android-Get video first frame thumbnail

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.