如何从 Android 中的视频文件路径创建视频缩略图?

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

我想从 SD 卡路径创建视频的缩略图。我该怎么做?

您可以使用ThumbnailUtils class 获取视频文件的视频缩略图。

createVideoThumbnail() 是 return 来自视频文件路径的视频位图(缩略图)的方法。

来自 Android 文档:

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.

您可以像这样从 sdcard 路径创建 VideoThumbnail。

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

使用ThumbnailUtils,您可以创建两种类型的缩略图。

  1. MediaStore.Images.Thumbnails.MICRO_KIND 类型将生成大小为 96 x 96 的缩略图。

  2. MediaStore.Images.Thumbnails.MINI_KIND 类型将生成大小为 512 x 384 的缩略图。

希望对您有所帮助!

请检查我的代码希望它能帮助你

/**
     * 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;
    }

根据需要修改以上方法

如果您是直接创建缩略图如下

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.


使用 SuziLoader

此加载程序将在后台加载本地存储在文件系统中的视频缩略图。

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

步骤 1. 将 JitPack 存储库添加到您的构建文件
将其添加到存储库末尾的根 build.gradle 中:

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

步骤 2. 添加依赖项

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

在清单中添加读取外部存储权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>