Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Jsoup获取图像并传递到ImageView_Java_Android_Jsoup - Fatal编程技术网

Java 如何使用Jsoup获取图像并传递到ImageView

Java 如何使用Jsoup获取图像并传递到ImageView,java,android,jsoup,Java,Android,Jsoup,你好 我正在尝试使用Jsoup检索图像,但我不确定应该从该网站获得什么。我使用了下面的代码从网站上阅读,并且能够得到图片的特定标题和它链接到的URL,但是没有得到图片 我想将此图像设置为活动中的ImageView。以下是我迄今为止的代码: // Get the required stuff from the webpage Document document = null; try { document = Jsoup.co

你好

我正在尝试使用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>

历史上最强的弟子Kenichi 544-女巫
所以我要的是那个网址上的真实图像

我该怎么办

谢谢

看看这是否有效:-

String temp = featureImage.getAttribute("style");
String url = temp.substring(temp.indexOf("(")+1,temp.indexOf(")"));

感谢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,以了解如何设置它。我最初尝试使用
    setImageURI()
    方法使用URL设置它,但这引发了一个错误。看看原因。相反,我使用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;
    
    }

  • 在此之后,我只需从前面设置
    位图
    ,并使用
    ASyncTask
    onPostExecute()
    方法更新图像视图:

    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();
    }
    

干杯

您可以使用正则表达式从样式属性中解析它吗?考虑到了这一点,但这似乎提供了一个良好的开端,但我认为您指的是getElementsByAttribute()和子字符串,而不是子字符串您可以在元素上调用getAttribute