如何使用Jsoup获得的图像并传递给ImageView的 [英] How to get an image using Jsoup and pass to an ImageView
问题描述
您好,
我想检索使用Jsoup的图像,但我不能确定,究竟我应该从网站上获得。我用下面的code从网站阅读并已经能够获得的图像特定的标题,并将其链接到URL,但不是图像。
I'm trying to retrieve an image using Jsoup but I'm unsure as to what exactly I should be getting from the website. I've used the following code to read from the website and have been able to get the images particular title and the URL it links to but not the image.
我想设置此图片的的ImageView
我有在活动。这里是我的code迄今:
I want to set this image to the ImageView
that I have in the activity. Here's my code thus far:
// 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
类,我看是这样的:
It should be noted that the image isn't stored as a normal img-src
way. The particular div
class I'm looking at is this:
<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中的实际图像后我。
So I'm after the actual image from that URL.
我怎么去呢?
感谢
推荐答案
感谢Hardip帕特尔提供的开始。下面是我所做的:
Thanks to Hardip Patel for providing the start. Here is what I did:
-
我把Hardips code和它更改为以下内容:
I took Hardips code and changed it to the following:
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(")"));
之后,花了小有一点希望有关计算器,了解如何设置。我最初tryed使用URL使用 setImageURI()
方法,它设置,但被扔一个错误。请参见为什么href=\"http://stackoverflow.com/questions/3870638/how-to-use-setimageuri-on-android\">。相反,我用的SoH的回答创建从URL的位图:
After that it took alittle looking about StackOverflow to find out how to set it. I initially tryed to set it using the URL using the setImageURI()
method, but that was throwing an error. See here for why. Instead I used that SoH's answer to create a bitmap from the 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()
方法:
After that I just had to set the Bitmap
from earlier and update the image view using the ASyncTask
's onPostExecute()
method:
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();
}
干杯所有!
这篇关于如何使用Jsoup获得的图像并传递给ImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!