How to make YouTube video thumbnails in android? [duplicate]
YouTube puts the thumbnails of the video at a specific predictable URL. It would be a bit of a pain, but I'm sure you could find a way to display the images from the URL, or to download them and then display them.
Here's information on my blog on what those thumbnail URLs are.
I'll copy and paste what I wrote in the blog post:
Look at the link for the video–for example, http://www.youtube.com/watch?v=GDFUdMvacI0
Take the video ID
… the portion after “v=”
, in this case GDFUdMvacI0
. If the URL is longer than that, only go until the next ampersand. For example, http://www.youtube.com/watch?v=GDFUdMvacI0&feature=youtu.be
is the same, GDFUdMvacI0
.
Then just substitute your video ID for the video ID in the following URLs to these thumbnail images:
- http://img.youtube.com/vi/GDFUdMvacI0/0.jpg
- http://img.youtube.com/vi/GDFUdMvacI0/1.jpg
- http://img.youtube.com/vi/GDFUdMvacI0/2.jpg
- http://img.youtube.com/vi/GDFUdMvacI0/3.jpg
0.jpg
is a full-sized image. The other three are very small (120×90) and are taken automatically by YouTube from three certain points in the video.
Download picasso jar file and put that jar file in "libs" folder
Use picasso to download image
Use method
extractYoutubeId(url)
to extract youtube id from YoutubeVideo Url
To get image of youtube video use given link and put youtube id in that url as below: "http://img.youtube.com/vi/"+extractYoutubeId(url)+"/0.jpg"
Youtube_Video_thumnail
package com.app.download_video_demo; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import com.squareup.picasso.Picasso; // get Picasso jar file and put that jar file in libs folder public class Youtube_Video_thumnail extends Activity { ImageView iv_youtube_thumnail,iv_play; String videoId; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); super.setContentView(R.layout.youtube_video_activity); init(); try { videoId=extractYoutubeId("http://www.youtube.com/watch?v=t7UxjpUaL3Y"); Log.e("VideoId is->","" + videoId); String img_url="http://img.youtube.com/vi/"+videoId+"/0.jpg"; // this is link which will give u thumnail image of that video // picasso jar file download image for u and set image in imagview Picasso.with(Youtube_Video_thumnail.this) .load(img_url) .placeholder(R.drawable.ic_launcher) .into(iv_youtube_thumnail); } catch (MalformedURLException e) { e.printStackTrace(); } } public void init() { iv_youtube_thumnail=(ImageView)findViewById(R.id.img_thumnail); iv_play=(ImageView)findViewById(R.id.iv_play_pause); } // extract youtube video id and return that id // ex--> "http://www.youtube.com/watch?v=t7UxjpUaL3Y" // videoid is-->t7UxjpUaL3Y public String extractYoutubeId(String url) throws MalformedURLException { String query = new URL(url).getQuery(); String[] param = query.split("&"); String id = null; for (String row : param) { String[] param1 = row.split("="); if (param1[0].equals("v")) { id = param1[1]; } } return id; } }
youtube_video_activity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout android:id="@+id/webvideo_layout2" android:layout_width="250dp" android:layout_height="180dp" android:layout_gravity="center" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" > <ImageView android:id="@+id/img_thumnail" android:layout_width="250dp" android:layout_height="180dp" android:layout_centerInParent="true" android:scaleType="fitXY" /> <ImageView android:id="@+id/iv_play_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/icn_play" /> </RelativeLayout></LinearLayout>
Try this
public static String getYoutubeThumbnailUrlFromVideoUrl(String videoUrl) { return "http://img.youtube.com/vi/"+getYoutubeVideoIdFromUrl(videoUrl) + "/0.jpg";}public static String getYoutubeVideoIdFromUrl(String inUrl) { inUrl = inUrl.replace("&feature=youtu.be", ""); if (inUrl.toLowerCase().contains("youtu.be")) { return inUrl.substring(inUrl.lastIndexOf("/") + 1); } String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(inUrl); if (matcher.find()) { return matcher.group(); } return null;}