Mainactivity is as follows
Package cn.testmediametadataretriever;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Android.media.MediaMetadataRetriever;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.CompressFormat;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button; /** * Demo Description: * Use Mediametadataretriever to intercept video at time * and convert to bitmap store in SDcard * Special NOTE: * Getframeattime () method The unit of the first parameter is microsecond (u
s) * * */public class Mainactivity extends activity {private Button Mbutton;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Init ();
private void Init () {mbutton= (Button) Findviewbyid (R.id.button);
Mbutton.setonclicklistener (New Clicklistenerimpl ()); Private class Clicklistenerimpl implements onclicklistener{@Override public void OnClick (View v) {switch (v. GetId ()) {case R.id.button:getbitmapsfromvideo ();
Default:break; }} public void Getbitmapsfromvideo () {String datapath = environment.getexternalstoragedirectory () +/te
Stvideo.mp4 ";
Mediametadataretriever retriever = new Mediametadataretriever ();
Retriever.setdatasource (DataPath);
Gets the length of the video (in milliseconds) String time = Retriever.extractmetadata (mediametadataretriever.metadata_key_duration);
Gets the length of the video (in seconds) int seconds = integer.valueof (time)/1000; Get Bitmap of every second moment like first second, for (int i = 1; I <= seconds i++) {Bitmap Bitmap = Retriever.getframeattime (i*1000*10
00,mediametadataretriever.option_closest_sync);
String path = environment.getexternalstoragedirectory () + file.separator + i + ". jpg";
FileOutputStream fos = null;
try {fos = new FileOutputStream (path);
Bitmap.compress (compressformat.jpeg, FOS);
Fos.close ();
catch (Exception e) {e.printstacktrace (); }
}
}
}
Main.xml is as follows:
<relativelayout
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 "
>
<button
android:id= "@+id/button"
android:layout_width= "Wrap_content"
android: layout_height= "Wrap_content"
android:text= "get frame picture for video"
android:layout_centerinparent= "true"
/>
</RelativeLayout>