How to get thumbnail image/icon for a youtube video

public static String getYoutubeThumbnailUrl(String youtubeUrl)
{
String thumbImageUrl = "http://img.youtube.com/vi/noimagefound/default.jpg";

if( youtubeUrl!=null && youtubeUrl.trim().length()>0 && youtubeUrl.startsWith("http") && youtubeUrl.contains("youtube"))
{
LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
try
{
youtubeUrl = URLDecoder.decode(youtubeUrl, "UTF-8");

if(youtubeUrl.indexOf('?')>0)
{
String array[] = youtubeUrl.split("\\?");

int equalsFilterIndex = array.length - 1;
String equalsString = array[equalsFilterIndex];

if(equalsString.indexOf('&')>0)
{
String ampersandArray[] = equalsString.split("&");
for (String parameter : ampersandArray)
{
String keyvaluePair[] = parameter.split("=");
params.put(URLDecoder.decode(keyvaluePair[0],"UTF-8"),URLDecoder.decode(keyvaluePair[1],"UTF-8"));
}
}
else
{
String v[] = equalsString.split("=");
params.put(URLDecoder.decode(v[0],"UTF-8"),URLDecoder.decode(v[1],"UTF-8"));
}
}

int size = params.size();

if(size==0 || !params.containsKey("v"))

{

if(size>0)
youtubeUrl = youtubeUrl.substring(0, youtubeUrl.indexOf("?",0));

String vtoSplit = "/v/";
int index = youtubeUrl.indexOf(vtoSplit,0);

int fromIndex = index + vtoSplit.length();
int lastIndex = youtubeUrl.indexOf("?", 0);

if(lastIndex==-1)
lastIndex = youtubeUrl.length();

String v = youtubeUrl.substring(fromIndex,lastIndex);
thumbImageUrl = "http://img.youtube.com/vi/" + v + "/default.jpg";
}
else
{
String v = params.get("v");
thumbImageUrl = "http://img.youtube.com/vi/" + v + "/default.jpg";
}
}
catch(Exception e)
{
if(e!=null)
e.printStackTrace();
}
}

return thumbImageUrl;
}
Advertisements

Tags: , ,

About krishjlk

http://www.facebook.com/krishna.kanth.121

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: