androidfileimageviewandroid-bitmapvideo-thumbnails

how can I show a video thumbnail from a video path?


I want to show a video thumbnail in an ImageView from a video path on storage. Is there a function that takes a video path and returns a bitmap of a thumbnail? I get the video path by this code:

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

Solution

  • It is the default way to create a thumbnail.

    For Mini Kind

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

    For Micro Kind

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

    Also, you can use Glide for Url as well as Video path of Device.

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

    also, you can resize thumbnail by using .override(50,50) with Glide.