Get thumbnail from Video URL

Droid By Me
1 min readApr 17, 2019

Hello coders,

Here we show you a code that can be helpful for getting thumbnail from video URL.

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {
Bitmap bitmap = null;
MediaMetadataRetriever mediaMetadataRetriever = null;
try {
mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
// mediaMetadataRetriever.setDataSource(videoPath);
bitmap = mediaMetadataRetriever.getFrameAtTime();
} catch (Exception e) {
e.printStackTrace();
throw new Throwable("Exception in retriveVideoFrameFromVideo(String videoPath)" + e.getMessage());

} finally {
if (mediaMetadataRetriever != null) {
mediaMetadataRetriever.release();
}
}
return bitmap;
}

Usage:

try {
bitmap = retriveVideoFrameFromVideo("https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4");
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}

If you want to change its width and height, then create scaledBitmap:

if (bitmap != null) {
bitmap = Bitmap.createScaledBitmap(bitmap, 240, 240, false);
imageView.setImageBitmap(bitmap);
}

Sometimes you will get MediaRetriever exception. So if you a have better solution, then write here and make this post more helpful.

Output

--

--