Create Thumbnail From Video in Android App

Thumbnails can easily be created in Android Application using ThumbnailUtils. If you want to create thumbnail from video, you need to specify file path for your video and thumbnail size MICRO or MINI type.

Using ThumbnailUtils, you can create thumbnail of two types.

1. MediaStore.Images.Thumbnails.MICRO_KIND type will generate thumbnail of size 96 x 96.

2. MediaStore.Images.Thumbnails.MINI_KIND type will generate thumbnail of size 512 x 384.

Creating thumbnail from video file path

/**
 * API for creating thumbnail from Video
 * @param filePath - video file path
 * @param type - size MediaStore.Images.Thumbnails.MINI_KIND or MICRO_KIND
 * @return thumbnail bitmap
 */
public Bitmap createThumbnailFromPath(String filePath, int type){
    return ThumbnailUtils.createVideoThumbnail(filePath, type);
}

Creating thumbnail from particular timeframe of Video

For creating thumbnail at particulat time,MediaMetadataRetriever class cane be used. It provides a unified interface for retrieving frame and meta data from an input media file.

/**
 * API for generating Thumbnail from particular time frame
 * @param filePath - video file path
 * @param timeInSeconds - thumbnail to generate at time
 * @return- thhumbnail bitmap
 */
private Bitmap createThumbnailAtTime(String filePath, int timeInSeconds){
    MediaMetadataRetriever mMMR = new MediaMetadataRetriever();
    mMMR.setDataSource(filePath);
    //api time unit is microseconds
    return mMMR.getFrameAtTime(timeInSeconds*1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
}

From existing Image file

You can also create thumbnail from existing image also. Just pass the path of image file along with width and height of the desired thumbnail.

/**
 * API for creating thumbnail of specified dimensions from an image file
 * @param filePath - source image path
 * @param width - output image width
 * @param height - output image height
 * @return - thumbnail bitmap of specified dimension
 */
private Bitmap createThumbnailFromBitmap(String filePath, int width, int height){
    return ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePath), width, height);
}

From a Bitmap file

Similarly you can also create thumbnail from bitmap also with passing bitmap as source.

/**
 * API for creating thumbnail from existing Bitmap
 * @param source - source Bitmap file
 * @param width - output image width
 * @param height - output image height
 * @return - thumbnail bitmap of specified dimension
 */
private Bitmap createThumbnailFromBitmap(Bitmap source, int width, int height){
    //OPTIONS_RECYCLE_INPUT- Constant used to indicate we should recycle the input
    return ThumbnailUtils.extractThumbnail(source, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}

That all for now. Please comment for more clarification.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *