Get image from description RSS tag
I'm getting RSS feed in my app. I would like to get the image from 开发者_高级运维the description tag.
So getting out the part
href="http://1.bp.blogspot.com/-cK4XpGZFqrw/TlzZVDN29EI/AAAAAAAAIP4/HIPTiLxMHB8/s1600/empoou.jpg"
from
<description><p><a href="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-Kk4CM/0/da"><img src="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-Kk4CM/0/di" border="0" ismap="true"></img></a><br/><ahref="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-k4CM/1/da"><imgsrc="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-Kk4CM/1/di" border="0" ismap="true"></img></a></p><a href="http://1.bp.blogspot.com/-cK4XpGZFqrw/TlzZVDN29EI/AAAAAAAAIP4/HIPTiLxMHB8/s1600/empoou.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 180px;" .............
I'm using sax parser to load the RSS. Any help to get the image? Thanks
Then when you have the URL of the image, just see this example:
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class Example1 extends Activity{
EditText inputUrl;
OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();
Editable ed = inputUrl.getText();
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView)findViewById(R.id.image1);
imgView.setImageDrawable(image);
}
};
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
inputUrl = ((EditText)findViewById(R.id.imageUrl));
inputUrl.setSingleLine();
inputUrl.setTextSize(11);
Button getImageButton = (Button)findViewById(R.id.getImageButton);
getImageButton.setOnClickListener(getImageBtnOnClick);
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
}
and full tutorial there: http://asantoso.wordpress.com/2008/03/07/download-and-view-image-from-the-web/
use this jsoup lib for get the value of HTML element attribute value
first, you need perform xml parsing then,
String description = rssFeed.getDescription("description");
Document doc = Jsoup.parse(html);
Elements img = doc.select("img");
String url = img.attr("src");
doc.select() is return multiple Element objects if there are more than one img elements, if you want to get img element more then 1st index then use following code,
String description = rssFeed.getDescription("description");
Document doc = Jsoup.parse(html);
Elements img = doc.select("img");
String url = getImgSrc(imgs);
private String getImgSrc(Elements imgs) {
for (int j = 0; j < imgs.size(); j++) {
Element img = imgs.get(j);
if (img.hasAttr("src")) {
return img.attr("src");
}
}
return null;
}
精彩评论