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

丶灬走出姿态 提交于 2019-11-27 21:38:47

问题


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


回答1:


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!




回答2:


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




回答3:


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"/>


来源:https://stackoverflow.com/questions/32517124/how-to-create-a-video-thumbnail-from-a-video-file-path-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!