Saturday, May 25, 2013

Get image frame in video using MediaMetadataRetriever

MediaMetadataRetriever, for API level 10 or higher, provides a unified interface for retrieving frame and meta data from an input media file.

Example to retrieve frame in video:

retrieve frame in video

package com.example.androidmediametadataretriever;

import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
 String uri = "/storage/sdcard0/DCIM/100MEDIA/VIDEO0007.mp4";

 protected void onCreate(Bundle savedInstanceState) {
  ImageView capturedImageView = (ImageView)findViewById(;
  MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
  Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(5000000); //unit in microsecond


- Get the current frame in VideoView using MediaMetadataRetriever

Permission of "android.permission.READ_EXTERNAL_STORAGE" is needed, otherwise MediaMetadataRetriever.setDataSource() will throw IllegalArgumentException.

Read: MediaMetadataRetriever: IllegalArgumentException thrown from setDataSource()


Student said...

I am getting only one frame/ current frame from mediametadataretriever.
I want to take all frames from a video which has been stored in sdcard.. How to do it..

Osc said...

How can I save that bitmap to sdcard?

Erik said...

hello Osc,

you have a bitmap, so you can save bitmap to sdcard.

Unknown said...

Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(5000000);

i am getting null value in bmFrame.. so what i do

Erik said...

hello sugumar.p sugu,

getFrameAtTime() return null means such a frame cannot be retrieved.

Unknown said...

hello, i tried the above code in android 4.0 (icecream sandwich) but it throwing fatal exception, can u pls help me

Erik said...

hello Ankita A,

Is it IllegalArgumentException thrown from the code mediaMetadataRetriever.setDataSource(uri)?

If yes, please read my update: MediaMetadataRetriever: IllegalArgumentException thrown from setDataSource().

Unknown said...

thank you, its working now, but am getting only one frame.i want to extract all frames. how to do it?

Anonymous said...

Does anyone get the solution for extracting all frames from video in android??