从描述 RSS 标签获取图像

发布于 2024-12-02 00:24:00 字数 1132 浏览 0 评论 0原文

我在我的应用程序中获取 RSS 源。我想从描述标签中获取图像。

取出部分

href="http://1.bp.blogspot.com/-cK4XpGZFqrw/TlzZVDN29EI/AAAAAAAAIP4/HIPTiLxMHB8/s1600/empoou.jpg"

因此,从

<description>&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-Kk4CM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-Kk4CM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;&lt;ahref="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-k4CM/1/da"&gt;&lt;imgsrc="http://feedads.g.doubleclick.net/~a/b4nArh9wvGYEh4cdSiHD3-Kk4CM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;a href="http://1.bp.blogspot.com/-cK4XpGZFqrw/TlzZVDN29EI/AAAAAAAAIP4/HIPTiLxMHB8/s1600/empoou.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 180px;" .............

我使用 sax 解析器加载 RSS 中 。有什么帮助获取图像吗?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-12-09 00:24:00

然后,当您获得图像的 URL 时,只需查看此示例:

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

以及完整教程:
http://asantoso.wordpress。 com/2008/03/07/download-and-view-image-from-the-web/

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/

与酒说心事 2024-12-09 00:24:00

使用这个 jsoup lib 首先获取 HTML 元素属性值的值

,然后需要执行 xml 解析,

String description = rssFeed.getDescription("description");
Document doc = Jsoup.parse(html);
Elements img = doc.select("img");
String url = img.attr("src");

doc.select( ) 如果有多个 img 元素,则返回多个 Element 对象,如果您想获取比第一个索引更多的 img 元素,请使用以下代码,

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文