Search code examples
androidimageviewpicassoandroid-glide

glide and picasso cannot load a picture by url


I have a link

https://www.imdb.com/title/tt0068646/mediaviewer/rm746868224

I need to download this picture by neither Glide nor Picasso can help me. They both return error. Examples how I use them

        Glide.with(Objects.requireNonNull(getContext()))
            .load(movieDetails.getImage())
            .fitCenter()
            .placeholder(R.drawable.wait_button)
            .error(R.drawable.error_download)
            .into(image);

    Picasso.get()
            .load(movieDetails.getImage())
            .fit()
            .placeholder(R.drawable.wait_button)
            .error(R.drawable.error_download)
            .into(image);

image - is my target ImageView
What do I do wrong? Or problem is in the url?
How can I download this pic by Glide or Picasso?
I have another type of url in my project and all of them end with ....jpg and everything is ok


Solution

  • The url is not to download the image. This https://www.imdb.com/title/tt0068646/mediaviewer/rm746868224 is not a valid url for poster image.

    Instead you need to parse the html and get the required image url.

    In the html you have the below and the url is in a meta tag

     <meta itemprop="image" content="https://m.media-amazon.com/images/M/MV5BM2MyNjYxNmUtYTAwNi00MTYxLWJmNWYtYzZlODY3ZTk3OTFlXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_QL50_SY500_CR0,0,352,500_AL_.jpg">
    

    For the url provided you can parse and get the link by parsing html using jsoup parser.

    Add the below to your app build.gradle file

     implementation group: 'org.jsoup', name: 'jsoup', version: '1.12.1'
    

    Use asynctask doInBackground

      Document document = Jsoup.connect("https://www.imdb.com/title/tt0068646/mediaviewer/rm746868224");
      Element  meta = document.select("meta[itemprop=image]").first()
      String image_url = meta.attr("content")
    

    In onPostEecute

       Glide.with(context).load(url) // url is the one returned in doInbackground
                .override(100, 200) // provide some width and height. glide will automatically scale the image down appropriately
                .into(image);
    

    Note: I guess there is api available from imdb to get poster images but i think its a paid licensed one. Not sure about the legality stuff.

    Similarly you can parse other tags under html and get the required information.

    Also i checked with a different url such as https://www.imdb.com/title/tt0068647/mediaviewer/rm2109407488 the meta has the image url. So this can be used for other urls as well.