Here are two methods to retrieve thumbnail from a video.
1. Using MediaMetadataRetriever
Works for local files and most URLs
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.util.Log;
import java.util.HashMap;
public class VideoThumbnailHelper {
public static Bitmap getVideoThumbnail(String videoUrl) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
Bitmap bitmap = null;
try {
if (Build.VERSION.SDK_INT >= 14) {
// Required for network URLs
retriever.setDataSource(videoUrl, new HashMap<String, String>());
} else {
retriever.setDataSource(videoUrl);
}
// Get frame at 1st second (1000000 microseconds)
bitmap = retriever.getFrameAtTime(1000000, MediaMetadataRetriever.OPTION_CLOSEST);
} catch (Exception e) {
Log.e("VideoThumb", "Error retrieving thumbnail: " + e.getMessage());
} finally {
retriever.release();
}
return bitmap;
}
}
Usage
String videoUrl = "https://www.example.com/sample.mp4";
Bitmap thumbnail = VideoThumbnailHelper.getVideoThumbnail(videoUrl);
if (thumbnail != null) {
imageView.setImageBitmap(thumbnail);
} else {
Toast.makeText(this, "Failed to get thumbnail", Toast.LENGTH_SHORT).show();
}
2. Using Glide (simpler, recommended for network URLs)
Add this dependency
implementation 'com.github.bumptech.glide:glide:4.16.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
Then use below code. Glide automatically extracts a representative frame from the video and displays it in the ImageView.
import com.bumptech.glide.Glide;
Glide.with(this)
.asBitmap()
.load("https://www.example.com/sample.mp4") // your video URL
.into(imageView);
MediaMetadataRetriever might fail for some remote URLs if they require authentication or have certain headers — in that case, Glide is more reliable. For large video files, avoid running this on the main thread — use a background thread or AsyncTask.