Search code examples
androidvideoandroid-gridview

how to show the videos in grid view using video url string


I have a URL string in the array. how to display the URL of the video in grid view and on click the grid plays in next screen. how to implement this? anyone guide me...

I tried for the image it works fine. How to implement for videos, the array is a string...URL...

public class act extends Activity {
/** Called when the activity is first created. */
//Integer[] imageIDs={R.drawable.icon,R.drawable.icon,R.drawable.icon};
String uri1="https://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
String uri2="https://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
String uri3="https://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
String[] urls={uri1,uri2,uri3};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GridView grd=(GridView)findViewById(R.id.gridView1);
    grd.setAdapter(new ImageAdapter(this));
    grd.setOnItemClickListener(new OnItemClickListener()
    {
    public void onItemClick(AdapterView<?> parent,View v,int pos,long id)
    {
        Toast.makeText(getBaseContext(),"pic"+(pos+1)+"select ",Toast.LENGTH_SHORT).show();
    }
    });
}
public class ImageAdapter extends BaseAdapter
{
    private Context context;
    private int itemBackground;
    ImageAdapter(Context c)
    {
    context=c;
    TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
    itemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
    a.recycle();
    }
    public int getCount()
    {
        return urls.length;
    }
    public Object getItem(int pos)
    {
        return pos;
    }
    public long getItemId(int pos)
    {
        return pos;
    }
    public View getView(int pos,View cv,ViewGroup vg)
    {
        ImageView imageview=new ImageView(context);
        imageview.setImageResource(urls.length);
        imageview.setScaleType(ImageView.ScaleType.FIT_XY);
        imageview.setLayoutParams(new GridView.LayoutParams(150,120));
        imageview.setBackgroundResource(itemBackground);
        return imageview;
    }
  }
}

Thanks a lot in advance...


Solution

  • You are wrong on this stage.

    How to display the url of video in gridview ?
    

    Don't try to print your URL into grid view item. YouTube is provide a thumbnail of specif video in different frame.So directly use that URL and play.

    Here is the Image URL for YouTube Thumbnail (order by Frame)

    YouTube URL : http://img.youtube.com/vi/

    Video_ID : AxeOPU6n1_M

    Frame Number : 0.jpg

    Final YouTube Thumbnail URL

    http://img.youtube.com/vi/AxeOPU6n1_M/0.jpg

    http://img.youtube.com/vi/AxeOPU6n1_M/1.jpg

    http://img.youtube.com/vi/AxeOPU6n1_M/2.jpg

    So, at the end you have to get youtube_img_url from web service so you can use it.

    And if you don't know how to set image coming from server then check out below links.

    Example - 1

    LazyList

    Both example is good now you have to just update your code as per requirement.

    How to play on click the grid plays in next screen. 
    

    Here its should be new activity called via intent and this will play in YouTube Player.

    @Override
            public void onCreate(Bundle savedInstanceState) 
            {
             super.onCreate(savedInstanceState);
              setContentView(R.layout.videoplaying);
              String url= "Your url";
              Uri uri=Uri.parse(url);
              VideoView video=(VideoView)findViewById(R.id.videoplaying);
              video.setVideoURI(uri);
              video.start();
            }