Thursday, July 10, 2014

Android : Get youtube video thumbnails

Android : Get youtube video thumbnails
This post about how to get YouTube videos thumbnails . pass the youtube video url and get  the thumbnails of the video set to imageview.
Source code


public String getYoutubeThumbnailUrl(String youtubeUrl)
    {
    String thumbImageUrl = "http://img.youtube.com/vi/noimagefound/maxresdefault.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 + "/maxresdefault.jpg";
    }
    else
    {
    String v = params.get("v");
    thumbImageUrl = "http://img.youtube.com/vi/" + v + "/maxresdefault.jpg";
    }
    }
    catch(Exception e)
    {
    if(e!=null)
    e.printStackTrace();
    }
    }
    
    return thumbImageUrl;
    }

This thumbnails for this video http://www.youtube.com/watch?v=FQZbDVaT39Q .




Full activity source code


com.vijay.youtubethumb;

import java.net.URLDecoder;
import java.util.LinkedHashMap;

import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
    ImageView imageView;
    ImageLoader imageLoader;
    DisplayImageOptions options;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_player);
              imageView = (ImageView)findViewById(R.id.imageView1);
              ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
            .threadPoolSize(2)
            .memoryCache(new WeakMemoryCache())
            .build();
            imageLoader = ImageLoader.getInstance();
            imageLoader.init(config);
            options = new DisplayImageOptions.Builder()
                     .cacheOnDisc()
                     .showStubImage(R.drawable.ic_launcher)
                           .showImageForEmptyUri(R.drawable.ic_launcher)
                     .build();
            imageLoader.displayImage(getYoutubeThumbnailUrl("http://www.youtube.com/watch?v=FQZbDVaT39Q"),imageView,options);
           
       }
      
       public String getYoutubeThumbnailUrl(String youtubeUrl)
    {
    String thumbImageUrl = "http://img.youtube.com/vi/noimagefound/maxresdefault.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 + "/maxresdefault.jpg";
    }
    else
    {
    String v = params.get("v");
    thumbImageUrl = "http://img.youtube.com/vi/" + v + "/maxresdefault.jpg";
    }
    }
    catch(Exception e)
    {
    if(e!=null)
    e.printStackTrace();
    }
    }
    
    return thumbImageUrl;
    }
}









Check out this may be help you

Related Posts Plugin for WordPress, Blogger...