简体   繁体   中英

How can i get video thumbnail from remote url in android

I am trying to get thumbnail from a URL Example: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" It is possible in IOS using AVAssets. The way i am doing it is using the following function

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {
        Bitmap bitmap = null;
        MediaMetadataRetriever mediaMetadataRetriever = null;
        try {
            mediaMetadataRetriever = new MediaMetadataRetriever();
            if (Build.VERSION.SDK_INT >= 14)
                mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
            else
                mediaMetadataRetriever.setDataSource(videoPath);
            //   mediaMetadataRetriever.setDataSource(videoPath);
            bitmap = mediaMetadataRetriever.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + e.getMessage());
        } finally {
            if (mediaMetadataRetriever != null) {
                mediaMetadataRetriever.release();
            }
        }
        return bitmap;
    }

But the issue is it's slow down the recycleview and load image again and again.

I have solve the issue by using GLide 4.x. I have find another solution by using MediaMetadataRetriever. But it's not feasible to use MediaMetadataRetriever in a recycle view because it runs on main thread and cause ANR. The code that works for me is below.

RequestOptions requestOptions = new RequestOptions();
                Glide.with(context)
                        .load("Your URL")
                        .apply(requestOptions)
                        .thumbnail(Glide.with(context).load("Your URL"))
                        .into(holder.img_video_attachment_preview);

Your own answer worked for me, and tweaking it a little bit, I ended up with a slightly shorter version. Although you might have had your own reasons for doing it that way...

GlideApp.with(context)
    .load(item.videoURL) // your video url
    .into(image_view)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM