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

3

22

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

Chowder answered 11/9, 2015 at 6:41 Comment(0)
O
42

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!

Oliana answered 11/9, 2015 at 6:44 Comment(8)
No it does not. it only works with video files that are in storageOliana
I have tried it, but it didn't work for the videos that are on SD Card (External Storage) on Android 5.0+. It returned null bitmapEttaettari
@Riten how are you getting filepath and on which andorid os?Oliana
How can I set the filepath from a video on the raw folder?Fewer
If you're getting null bitmap then try to run it in real device and not in emulator. I wasted hours in emulator testing.Aldas
Chances of getting out of memory errors are high on this.Stefa
how about the .flv or .avi file?Four
doen't help in my case :(Sliwa
P
4

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"/>
Pneumococcus answered 1/10, 2017 at 8:52 Comment(0)
D
3

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

Dalrymple answered 11/9, 2015 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.