Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load video thumbnails using square picasso library?

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()?

like image 359
Mohammad Sharaf Ali Avatar asked Jun 04 '14 13:06

Mohammad Sharaf Ali


People also ask

How will you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.

Can I use Picasso image?

For using Picasso in the android project, we have to add a dependency in the app-level gradle file. So, For adding dependency open app/build. gradle file in the app folder in your Android project and add the following lines inside it.


1 Answers

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 :)

like image 119
Ram Avatar answered Oct 18 '22 08:10

Ram