Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace youtube iframe tag or thumbnails with ImageViews

I' am working on app that's get content from blog pages as jsoup nodes/elements, some pages contains youtube embedded tag iframe

like this


    <iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/VXD6a_LgBaQ/0.jpg" frameborder="0" height="400" src="https://www.youtube.com/embed/VXD6a_LgBaQ?feature=player_embedded" width="600"></iframe>

I'm looking for a way to replace any iframe youtube tags with imageView and display the thumbnail on it, that's when the user clicks on the image, the application opens the YouTube application through it intent and view the video.

I found answer someone here , he could replace img tags with Image Views, I succeed on get youtube link and thumbnail and view the image on ImageView

Element element = document.body();

                String youtubeThumbnailImageSrc = element.getElementsByClass
                        ("YOUTUBE-iframe-video").attr("data-thumbnail-src");

                String youTubeLink =
                        element.getElementsByClass("YOUTUBE-iframe-video").attr("src");

                Log.e("YouTube thumbnail", youtubeThumbnailImageSrc);
                Log.e("Youtube link", youTubeLink);

                if (youtubeThumbnailImageSrc.isEmpty()) {
                    youtubeThumbnailImagesetVisibility = 8;
                    intent.putExtra("youtubeThumbnailImagesetVisibility",
                            youtubeThumbnailImagesetVisibility);
                } else {
                    intent.putExtra("youTubeThumbnail", youtubeThumbnailImageSrc);
                    intent.putExtra("youTubeLink", youTubeLink);
                }

the destination it's supposed to be like this

like image 564
Dr Mido Avatar asked Jan 12 '19 21:01

Dr Mido


1 Answers

When you call

 String youtubeThumbnailImageSrc = element.getElementsByClass
                    ("YOUTUBE-iframe-video").attr("data-thumbnail-src");

You will get value of attribute only for the first element.

You need to iterate manually to get all links:

    for (Element e : element.getElementsByClass
            ("YOUTUBE-iframe-video")) {
        String youtubeThumbnailImageSrc = e.attr("data-thumbnail-src");
        System.out.println(youtubeThumbnailImageSrc);
    }
like image 52
Luk Avatar answered Sep 19 '22 10:09

Luk