如何从视频路径显示视频缩略图?

joh*_*ohn 4 android file imageview video-thumbnails android-bitmap

我想从存储上的视频路径在 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;
}
Run Code Online (Sandbox Code Playgroud)

Far*_*ana 8

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

迷你型

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

对于微类

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

此外,您可以将 Glide 用于 Url 以及设备的视频路径。

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

另外,还可以通过调整缩略图.override(50,50)滑翔


Mil*_*ada 6

使用Glide 库

显示本地存储的缩略图

String filePath = "/storage/emulated/0/Pictures/example_video.mp4";

GlideApp  
    .with(context)
    .asBitmap()
    .load(Uri.fromFile(new File(filePath)))
    .into(imageViewGifAsBitmap);
Run Code Online (Sandbox Code Playgroud)