0

I am fetching title, date and image from an online xml file and storing it in a arraylist. I have taken 3 different arraylists for storing title , date and image.

I am successful in getting image urls , my problem is how to download image from the url and show it in a imageview. The code for fetching the url is below.

  if((xpp_name.equals("content")))
  {                                                 
xpp.next(); 
    strvalue=xpp.getText();
    if(strvalue!=null){  
        String url = strvalue.substring(
                     strvalue.indexOf("src")+5,, strvalue.indexOf("jpg")+3);
     arrayList_for_images.add(url);
    }  
    xpp.next();     
    xpp.next();}                                                                                                                    
AggelosK
  • 4,313
  • 2
  • 32
  • 37
user1740281
  • 383
  • 2
  • 5
  • 16

3 Answers3

0

Here you can find a lazy loading library to download images from a URL and show them in a ListView. Also i suggest you to avoid using 3 different Arraylists and use only one ArrayList, instead, with a custom class that is used to store the information of an image(title, date and image url).

EDIT: Here is an example class that holds the information needed for the image:

import java.util.Date;

public class ImageObject {

    private String image_name;
    private String image_url;
    private Date image_date;

public ImageObject() {
}

public ImageObject(String image_name, String image_url, Date image_date) {

    this.image_name = image_name;
    this.image_url = image_url;
    this.image_date = image_date;
}

public void setImageName(String image_name) {

    this.image_name = image_name;
}

public void setImageURL(String image_url) {

    this.image_url = image_url;
}

public void setImageDate(Date image_date) {

    this.image_date = image_date;
}


public String getImageName() {

    return this.image_name;
}

public String getImageURL() {

    return this.image_url;
}

public Date getImageDate() {

    return this.image_date;
}
}

Here is an example usage in witch i initiate a ArrayList with one ImageObject:

ArrayList<ImageObject> data = new ArrayList<ImageObject>();
ImageObject obj = new ImageObject("test.jpg", "www.imageurl.com", new Date());
data.add(obj);

And this is how you retrieve a value from a certain ImageObject object from the ArrayList:

data.get(0).getImageName(); 
AggelosK
  • 4,313
  • 2
  • 32
  • 37
0

Start a new thread for downloading image from Url and set image in imageview in handler.

  private Bitmap pop_up_image_bitmap ;
  private  Handler handler ;
  private Runnable r;
  new Thread(new Runnable() {
        @Override
        public void run() {

            URL newurl;
            try {
                newurl = new URL(<Url>);
                pop_up_image_bitmap = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
                handler.post(r);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 


        }
    }).start();

Put handler :

    handler = new Handler();
     r = new Runnable()
    {
        public void run() 
        {
            if(pop_up_image_bitmap!=null){
            imageview.setImageBitmap(pop_up_image_bitmap);
            handler.postDelayed(this, 1000);
            popUpImageProgressBar.setVisibility(View.INVISIBLE);
            }
        }
    };

    handler.postDelayed(r, 1000);
Akhilesh Mani
  • 3,502
  • 5
  • 28
  • 59
0

try out this best image loading library ever.

https://github.com/nostra13/Android-Universal-Image-Loader

dharmendra
  • 7,835
  • 5
  • 38
  • 71