How parse image with jsoup

后端 未结 2 620
情歌与酒
情歌与酒 2021-01-07 01:24

I can\'t find any solution for this!! I need parsing a html page with jsoup and i need parse the image too but i can\'t do it! This is my MainActivity



        
相关标签:
2条回答
  • 2021-01-07 01:47

    Break it into pieces -

    1. Get the url of the image from div tag
    2. Show that image in the list view

    Now for "1", you can use jsoup and parse the url of the image

    For "2", first add image view to your list view xml Then create an asynctask, pass image url to it and reference of image view and set its value in onPostExecute. You said that website sets image dynamically, in that case every time on the get view method of list view adapter, you have to parse the webpage. Although this sounds very inefficient and intensive, but I don't think there is any other way. If somehow you can know when is the image going to change, then you can do this processing on that event. Anyway, this is the code of the asynctask to download the image from a url and set it to image view

    public class ThumbnailDownloader extends AsyncTask<String, Integer, Bitmap>{
    
    ImageView imview;
    Context ctx;
    
    public ThumbnailDownloader(Context c, ImageView imview){
        this.imview = imview;
        this.ctx = c;
    }
    
    @Override
    protected Bitmap doInBackground(String... urls) {
            try{
                HttpURLConnection connection = (HttpURLConnection)new URL(getThumbUrl(urls[0])).openConnection();
    
                connection.connect();
                InputStream input= connection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(input);
            return bitmap;
    
            }
            catch (Exception f) {
                // TODO Auto-generated catch block
                f.printStackTrace();
            }
        return null;
    }
    
    @Override
    protected void onPostExecute(Bitmap result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if(result != null && imview!=null){
            imview.setImageBitmap(result);
        }
    }
    
    public String getThumbUrl(String videoUrl){
        return "http://img.youtube.com/vi/"+videoUrl+"/default.jpg";
    }
    }
    

    The getThumbUrl method you have to modify according to your need. The one specified above is used to download youtube video thumbnails.

    0 讨论(0)
  • 2021-01-07 01:58

    check thease links:

    how-can-i-download-an-image-using-jsoup

    download-images-from-a-website-using-jsoup

    0 讨论(0)
提交回复
热议问题