How to load video thumbnails using square picasso library?

android, picasso

Solution

First You need to create VideoRequestHandler

public class VideoRequestHandler extends RequestHandler{
    public String SCHEME_VIDEO="video";
    @Override
    public boolean canHandleRequest(Request data) 
    {
        String scheme = data.uri.getScheme();
        return (SCHEME_VIDEO.equals(scheme));
    }

    @Override
    public Result load(Request data, int arg1) throws IOException 
    {
         Bitmap bm;
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
         try {
            Size size = new Size(250, 250);
            bm = ThumbnailUtils.createVideoThumbnail(new File(data.uri.getPath()), size, null);
         } catch (IOException e) {
            e.printStackTrace();
        }
        }
        else
        {
         bm = ThumbnailUtils.createVideoThumbnail(data.uri.getPath(), MediaStore.Images.Thumbnails.MINI_KIND);
        }
         return new Result(bm,LoadedFrom.DISK);  
    }
}

After That

 VideoRequestHandler videoRequestHandler;
 Picasso picassoInstance;

Build only once

 videoRequestHandler = new VideoRequestHandler();
 picassoInstance = new Picasso.Builder(context.getApplicationContext())
  .addRequestHandler(videoRequestHandler)
  .build();

Then load file from path

 picassoInstance.load(VideoRequestHandler.SCHEME_VIDEO+":"+filepath).into(holder.videoThumbnailView);

Updated October 2020

ThumbnailUtils.createVideoThumbnail(data.uri.getPath(), MediaStore.Images.Thumbnails.MINI_KIND);

is deprecated in Android Q. I will write in Kotlin:

val bm = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ThumbnailUtils.createVideoThumbnail(
                File(data.uri.path!!),
                Size(200f.toPx(), 200f.toPx()),
                CancellationSignal()
            )
        } else {
            ThumbnailUtils.createVideoThumbnail(
                data.uri.path!!,
                MediaStore.Images.Thumbnails.MINI_KIND
            )
        }

to Px is an extension function which is like below;

fun Float.toPx() =
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics)
        .toInt()

You can use any dp value for it :) I hope this can help you :)

Problem

Currently I'm loading MediaStore Image Thumbnails using picasso into the `ListView` with the following snippet: (`video.getData()` returns the actual path of the image such as `mnt/sdcard/...`) ``` Picasso.with(this.context) .load(new File(photo.getData())) .resize(50, 50).config(config) .centerCrop() .into(viewHolder.imageViewItem); ``` Now I'm and unable to load the MediaStore Video Thumbnails by just passing the `video.getData()` instead of `photo.getData()`?

Original source

Related problems