Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a video thumbnail from a video file path in Android?

I want to create a thumbnail of a video from the SD card path. How can I do that?

like image 498
Parth Bhayani Avatar asked Sep 11 '15 06:09

Parth Bhayani


3 Answers

You can use ThumbnailUtils class to get Video thumbnail of Video file.

createVideoThumbnail() is method which return Bitmap (thumbnail) of video from video file path.

From Android Docs:

public static Bitmap createVideoThumbnail (String filePath, int kind)

Create a video thumbnail for a video. May return null if the video is corrupt or the format is not supported.

You can create VideoThumbnail from sdcard path like this.

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);

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.

I hope it helps!

like image 58
Rajesh Avatar answered Nov 19 '22 05:11

Rajesh


If you are directly creating thumbnails as follows

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
    MediaStore.Images.Thumbnails.MINI_KIND);

Then there is a problem with this method if your are creating thumbnails for large video set(for large number of videos). the application will freeze until all the thumbnails are loaded because all the process are executing in the main thread.


Use SuziLoader

This loader will load the thumbnails for the videos which is locally stored on your filesystem in background.

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/video.mp4";
ImageView mThumbnail = (ImageView) findViewById(R.id.thumbnail);

SuziLoader loader = new SuziLoader(); //Create it for once
loader.with(MainActivity.this) //Context
    .load(path) //Video path
    .into(mThumbnail) // imageview to load the thumbnail
    .type("mini") // mini or micro
    .show(); // to show the thumbnail

To get this dependency use the following steps

Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    compile 'com.github.sushinpv:SuziVideoThumbnailLoader:0.1.0'
}

ADD READ EXTERNAL STORAGE Permission in manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
like image 33
Sushin Pv Avatar answered Nov 19 '22 05:11

Sushin Pv


Please check my code hope it will help you

/**
     * Retrieve video frame image from given video path
     * 
     * @param p_videoPath
     *            the video file path
     * 
     * @return Bitmap - the bitmap is video frame image
     * 
     * @throws Throwable
     */
    @SuppressLint("NewApi")
    public static Bitmap retriveVideoFrameFromVideo(String p_videoPath)
            throws Throwable
    {
        Bitmap m_bitmap = null;
        MediaMetadataRetriever m_mediaMetadataRetriever = null;
        try
        {
            m_mediaMetadataRetriever = new MediaMetadataRetriever();
            m_mediaMetadataRetriever.setDataSource(p_videoPath);
            m_bitmap = m_mediaMetadataRetriever.getFrameAtTime();
        }
        catch (Exception m_e)
        {
            throw new Throwable(
                    "Exception in retriveVideoFrameFromVideo(String p_videoPath)"
                            + m_e.getMessage());
        }
        finally
        {
            if (m_mediaMetadataRetriever != null)
            {
                m_mediaMetadataRetriever.release();
            }
        }
        return m_bitmap;
    }

Modify above method according to your need

like image 3
Android Team Avatar answered Nov 19 '22 06:11

Android Team