Friday, June 24, 2011

Android RSS reader with image

Since posting a link to my simple RSS Reader for Android implemented using a custom SAX parser I have some questions regarding upgrading to handle images - I have previously posted on how to implement new tags in the feed that you want to handle (including nested tags) - but one question I received was regarding when images are just embedded in the text (e.g. not a specific image tag, but say as part of a description as HTML).

So, I have updated the code (uploaded, see the sidebar) - this time it parses the description text data to retrieve any Image links its pretty grim string manipulation, but it does the job!


As you can see, it now parses the image URL from the description and inserts it to the left of the feed item.
What did I change?

First I needed to change the Article class to parse the link. I updated the setDescription method to also check for IMAGE tags to parse, if any found then it would set the ImgLink member:


  1. public void setDescription(String description) {  
  2.  this.description = description;  
  3.    
  4.  //parse description for any image or video links  
  5.  if (description.contains("<img ")){  
  6.   String img  = description.substring(description.indexOf("<img "));  
  7.   String cleanUp = img.substring(0, img.indexOf(">")+1);  
  8.   img = img.substring(img.indexOf("src=") + 5);  
  9.   int indexOf = img.indexOf("'");  
  10.   if (indexOf==-1){  
  11.    indexOf = img.indexOf("\"");  
  12.   }  
  13.   img = img.substring(0, indexOf);  
  14.     
  15.   setImgLink(img);  
  16.     
  17.   this.description = this.description.replace(cleanUp, "");  
  18.  }  
  19. }  



I then also had to update the RssListAdapter class - this needed the necessary action to pull down the link based on our parsed URL and then inflate the ImageView in our row:

  1. if (jsonImageText.get("imageLink") != null){  
  2.  String url = (String) jsonImageText.get("imageLink");  
  3.        URL feedImage= new URL(url);  
  4.       
  5.     HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();  
  6.        InputStream is = conn.getInputStream();  
  7.        Bitmap img = BitmapFactory.decodeStream(is);  
  8.        imageView.setImageBitmap(img);  
  9. }  

11 comments:

  1. I can't find the source code? :(

    ReplyDelete
  2. Hi Kenny

    Please find the Source code here
    http://www.thirdmindmedia.co.uk/source/android/SimpleAndroidRSSReader_0.2.zip

    Thanks for reply

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi Krishna
    is there a possibilty if you wan't to read a certain topic you can open it by a simple click?

    thanks for reply

    ReplyDelete
    Replies
    1. Hi Kevin
      Open the particular link in the onclick event of the list

      Delete
  5. I am used this code but mostly finding blank titles some times.
    After some titles it display the data .

    so canyou please guide me where is the error.?

    ReplyDelete
  6. Hey Krishna if U still helping on this code please reply
    have some problems
    its urgent

    ReplyDelete
  7. Could anyone send me the whole code on my email.
    tounsihouweida@gmail.com

    thanks

    ReplyDelete
  8. There are problems when rss feed don't containt an image... How to fix it? Please, help me :(

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete