2011-08-30 58 views
0

我在我的應用中獲取RSS提要。我想從描述標記中獲取圖像。從描述中獲取圖片RSS標記

所以從

<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;" ............. 

走出部分

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

我使用SAX解析器來加載RSS。任何幫助獲取圖像?由於

+0

通過獲得圖像您是說,下載? – jDourlens

+0

我想在用戶閱讀新聞 – kostas

+0

時再提供圖片嗎? – kostas

回答

1

然後當你有圖片的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/

+0

謝謝!!我會盡快檢查它! – kostas

+0

我的問題是,我不知道如何從描述中獲取鏈接... – kostas

+0

所以問題出在解析器中,而不是顯示圖像的方式? – jDourlens

2

使用這種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元素,如果你想獲得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; 
}