1

再会,

我正在尝试使用 Jsoup 检索图像,但我不确定我应该从网站获得什么。我已经使用以下代码从网站读取,并且能够获取图像的特定标题和它链接到的 URL,但不是图像。

我想将此图像设置为ImageView我在活动中拥有的图像。到目前为止,这是我的代码:

        // Get the required stuff from the webpage
        Document document = null;
        try {
            document = Jsoup.connect(URL).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Element info = document.select("div.featurebox").first();
        // Caption on image
        docInfo = info.text();
        // URL of image
        imageURL = info.attr("data-url");

        // Retrieve the actual image
        Element featureImage = document.select("div.featurebox-image").first();
        // Unsure what to get here

应该注意的是,图像不是以正常img-src方式存储的。div我正在看的特定课程是这样的:

<div class="featurebox-image" style="background:url(http://img.mangastream.com/cdn/feature/02.jpg) center center;">
                <div class="featurebox-caption">
                    <strong>History's Strongest Disciple Kenichi <em>544</em></strong> - Witch                    </div>
            </div>

所以我在寻找来自那个 URL 的实际图像。

我该怎么做?

谢谢

4

3 回答 3

1

看看这是否有效: -

String temp = featureImage.getAttribute("style");
String url = temp.substring(temp.indexOf("(")+1,temp.indexOf(")"));
于 2013-10-26T23:02:21.620 回答
1

感谢 Hardip Patel 提供的开始。这是我所做的:

  • 我采用了 Hardips 代码并将其更改为以下内容:

        Element featureImage = document.select("div.featurebox-image")
                .first();
        String temp = featureImage.getElementsByAttribute("style")
                .toString();
        // URL of image
        imageStrg = temp
                .substring(temp.indexOf("(") + 1, temp.indexOf(")"));
    
  • 之后,花了一点时间查看 StackOverflow 以了解如何设置它。我最初尝试使用该方法使用 URL 设置它setImageURI(),但这会引发错误。请参阅此处了解原因。相反,我使用 SoH 的答案从 URL 创建位图:

    // Method to return a bitmap from an images URL
    private Bitmap getImageBitmap(String url) {
    Bitmap bm = null;
    try {
    
        // See what we are getting
        Log.i(TAG, "" + url);
    
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
    
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
    
        bis.close();
        is.close();
    } catch (IOException e) {
        Log.e(TAG, "Error getting bitmap", e);
    }
    return bm;
    

    }

  • 之后我只需要设置from 并使用'方法Bitmap更新图像视图:ASyncTaskonPostExecute()

    imageOne = getImageBitmap(imageStrg);
    
    @Override
    protected void onPostExecute(String result) {
        // Write the result (document title) to the textview
        super.onPostExecute(result);
    
        // Update the textview with results
        if (result == null) {
    
            txtVwDocTitleValue.setText("Nothing to report...");
        } else {
            txtVwDocTitleValue.setText(result);
            txtVwDocURLValue.setText(imageURL);
    
            // Set the views image
            imgVwManga1.setImageBitmap(imageOne);
        }
        // Destroy the progress bar
        stopProgressDialog();
    }
    

大家干杯!

于 2013-10-27T15:39:10.887 回答
0

试试这个

文档 doc = Jsoup.connect("www.mywebsite.com").get();
元素图像 = doc.select("img[src~=(?i)\.(png|jpe?g|gif)]");

于 2021-09-24T12:45:38.467 回答