Search code examples
androidvideoyoutubeimageviewthumbnails

How to get video thumbnail from YouTube URL and set it to image view in android


I want to create an application in which I want to play YouTube video. I want to get thumbnail from YouTube video URL which I have currently play in application and set it to image view. can any body help me with this.


Solution

  • You can use YouTube API v3 to retrieve related video thumbnail info like title, image and length:

    https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet&fields=items/snippet(title,thumbnails),items/contentDetails/duration&key={{ YOUR_API_KEY }}&id={{ YOUR_YOUTUBE_VIDEO_ID }}
    

    Or this url if you only want to get only thumbnail image with medium size:

    https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet&fields=items/snippet(title,thumbnails/medium/url),items/contentDetails/duration&key={{ YOUR_API_KEY }}&id={{ YOUR_YOUTUBE_VIDEO_ID }}
    

    Your response may look like this:

    {
     "items": [
      {
       "snippet": {
        "title": "F..k This S..t I'm Out",
        "thumbnails": {
         "medium": {
          "url": "https://i.ytimg.com/vi/5FjWe31S_0g/mqdefault.jpg"
         }
        }
       },
       "contentDetails": {
        "duration": "PT25S"
       }
      }
     ]
    }