首页 > 解决方案 > 如何显示视频路径中的视频缩略图?

问题描述

我想在 ImageView 中显示来自存储视频路径的视频缩略图。是否有一个函数可以获取视频路径并返回缩略图的位图?我通过以下代码获取视频路径:

public ArrayList<String> getAllMedia() {
  HashSet<String> videoItemHashSet = new HashSet<>();
  String[] projection = {MediaStore.Video.VideoColumns.DATA, MediaStore.Video.Media.DISPLAY_NAME};
  Cursor cursor = getContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
  try {
    cursor.moveToFirst();
    do {
      videoItemHashSet.add((cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
    } while(cursor.moveToNext());
    cursor.close();
  } catch(Exception e) {
    e.printStackTrace();
  }
  ArrayList<String> downloadedList = new ArrayList<>(videoItemHashSet);
  return downloadedList;
}

标签: androidfileimageviewandroid-bitmapvideo-thumbnails

解决方案


这是创建缩略图的默认方式。

迷你类

Bitmap thumb;
//MINI_KIND, size:  512 x 384 thumbnail 
    thumb = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
            img_tumbnail.setImageBitmap(thumb);

微类

Bitmap thumb;
//MICRO_KIND, size: 96 x 96 thumbnail
thumb= ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MICRO_KIND);
img_tumbnail.setImageBitmap(thumb);

此外,您可以将 Glide 用于 Url 以及 Device 的 Video path。

Glide.with(context).with(this)
                    .asBitmap()
                    .load(videoFilePath) // or URI/path
                    .into(imgView); //imageview to set thumbnail to

.override(50,50)此外,您可以使用Glide调整缩略图的大小。


推荐阅读