Android uses the Thumbnailutils class to get the video first frame thumbnail

Source: Internet
Author: User
Tags event listener

First, to introduce the Thumbnailutils class, which is located under the Android.media package, this class has a public parameterless constructor, there are three static public methods, one to get the first frame of the video to get the bitmap, the other two methods used to thumbnail image processing.

1. Non-parametric constructor:thumbnailutils()

2. Three static public methods:

(1). How to create a video thumbnail:

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, respectively.

(2). Create a two method of a bitmap of the specified size:

Extractthumbnail (Bitmap source, int width, int height, int options) Extractthumbnail (Bitmap source, int width, int height)

The above two methods of handling bitmap size, wherein the parameters of the description, source for the bitmap,width to be processed as the width, height, options for the operation, if the options are defined as Thumbnailutils.options_ Recycle_input, it is to reclaim resources. Both of these methods can be intercepted to the first frame of the video bitmap to any desired size. The last method can also get a thumbnail image of the memory card.

3. If you do not understand the above method, you can view the API, see the address: Click the Open link, here I enclose a description of the method described in the API:



Two. Before writing our Android project, take the following three steps:

1. First of all, we first open the Android simulator, where the Android version of the emulator is not the same as the path of storage resources, for example, the Android 2.x system resource storage path for the/mnt/sdcard,android 4.x system storage path is/ Storage/sdcard.

2. Next, we will open a view named file Explorer, which we can find in Window-show view under the toolbar, as shown in:

3. After opening in the lower console there is a more view, because we have turned on the emulator, so this view has a lot of Android files, because my Android simulator is 4.x version, so I store the resources of the address is/storage/sdcard/, We place a video in the folder under this directory, as shown in:

Note: After the placement is successful, there is a video file under the directory, and the placement of unsuccessful messages will pop up the message in the console, usually because the directory where the video file is stored does not have permissions.



Three. Next we can write our Android project and create a new Android project:

1. First, open the layout file, layout file layout, the Activity_main file code is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical" >        <imageview         android:id= "@+id/imageview"        android:layout_ Width= "Wrap_content"        android:layout_height= "wrap_content"        android:src= "@drawable/ic_launcher"        android:layout_gravity= "center"/>        <button         android:id= "@+id/button"        android:layout_width= " Match_parent "        android:layout_height=" wrap_content "        android:text=" get video thumbnails "/></linearlayout>


2. Next, open the Mainactivity class with the following code:

Package Com.android_thumbnailutils;import Java.io.file;import Android.app.activity;import android.graphics.Bitmap; Import Android.media.thumbnailutils;import Android.os.bundle;import android.provider.MediaStore.Video.Thumbnails; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;public class Mainactivity extends Activity {private ImageView imageview;//declares the ImageView object private button button;//declares the Button object private Bitmap Bitmap; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ImageView = (ImageView) Findviewbyid (R.id.imageview);//Gets the ImageView control in the layout manager Button = (button) Findviewbyid (R.id.button);// Gets the button object in the layout Manager file File = new file ("/storage/sdcard/movies/music1.mp4");//Instantiates a file object that specifies the path to/storage/sdcard/ movies/music1.mp4//If the file exists if (file.exists ()) {bitmap = Thumbnailutils.createvideothumbnail (file.getabsOlutepath (), thumbnails.micro_kind);//Create a video thumbnail bitmap=thumbnailutils.extractthumbnail (bitmap, 200, 200, Thumbnailutils.options_recycle_input);//Specify the size of the video thumbnail}//Add button click event Listener button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {///gets bitmap NOT NULL if (bitmap! = null) {Imageview.setimagebitmap (bitmap);// Set the displayed picture for the video thumbnail toast.maketext (mainactivity.this, "Get video thumbnail Success", Toast.length_short). Show ();//popup message Prompt box} else { Toast.maketext (Mainactivity.this, "Get video thumbnail failed", Toast.length_short). Show ();//popup Message Prompt}}});}}

Note: If your Android emulator version is an Android 2.x system, the file path should be changed to/mnt/sdcard/music1.mp4 or/sdcard/ Music1.mp4, it's important to note that you should open your emulator and look at the file Explorer under view to see where your SD card is stored.

3. Finally, remember to add permission to access the SD card in the Androidmanifest.xml file, simply add this line of code to this file:

<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>



Four, next deploy this project to the Android emulator, the operation effect is as follows:

Click the Get Video thumbnail button as shown in:



5. The above content is only for your study reference, write not good, please forgive me, if there are errors, please point out, thank you!




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android uses the Thumbnailutils class to get the 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.