In the video player often see, there are a lot of video thumbnails, this article is about the Thumbnailutils class to get the thumbnail image of the video file;
Run as follows:
The code is as follows:
Mainactivity.java:
PackageCom.vrinux.thumbnailutilsdemo;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;ImportAndroid.media.ThumbnailUtils;ImportAndroid.os.Bundle;ImportAndroid.os.Environment;ImportAndroid.provider.MediaStore;ImportAndroid.widget.ImageView; Public class mainactivity extends Activity { //Define a ImageView object imgobj; PrivateImageView imgobj;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Get object imgobj;Imgobj = (ImageView) Findviewbyid (R.id.imgid);//Define a video file path;String FilePath =NULL;//The name of the video file;String filename ="VIDEO.3GP";//Determine if the sdcard is mounted; Because the Android version of the Mount path is not the same, it is recommended to use a method of judgment; if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//Get the path to the SDcard rootString Sdpath = Environment.getexternalstoragedirectory (). GetPath ();//SDcard The path of the video file by assembling the root path and file name;FilePath = Sdpath +"/"+ filename;//Call the method defined below to get the video of the specified size; //The final parameter I am not very clear what is meant, just so use, its source code is not open source;Bitmap Bitmap = Getvidiobitmap (FilePath, -, -, MediaStore.Images.Thumbnails.MICRO_KIND);//The bitmap to be obtained is displayed with Imgobj;Imgobj.setimagebitmap (bitmap); } } PublicBitmapGetvidiobitmap(String FilePath,intWidthintHeightintKind) {//define a bitmap to resemble bitmap;Bitmap Bitmap =NULL;The captured images of the//thumbnailutils are kept in the original proportions, but I find that sometimes some of the ImageView controls are not shown; //Adjust the Thumbnailutils method of the Createvideothumbnail to obtain a screenshot of the video;Bitmap = Thumbnailutils.createvideothumbnail (FilePath, kind);//Adjust the Thumbnailutils method extractthumbnail the original image (that is, the image captured above) to the specified size; //Last parameters of the definition I'm not quite sure, because it's closed source;Bitmap = Thumbnailutils.extractthumbnail (bitmap, width, height, thumbnailutils.options_recycle_input);//Put back bitmap object; returnBitmap }}
Activity_main.xml:
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Fill_parent"android:layout_height="Fill_parent" Android:orientation="vertical" > <ImageViewandroid:id="@+id/imgid"android:layout_width= "Wrap_content" android:layout_height="wrap_content" /> </linearlayout>
Android: Get thumbnails of video files through the Thumbnailutils class