Question

Im new to droid programming and i got a simple retrieve image from url working but confused on how to make it so i can load multiple images from my webpages url. Someone informed me change the drawable to string but not sure 100% how to do so here is most of my code so far:

public class Gallery extends Activity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

        ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
        Drawable drawable = LoadImageFromWebOperations("http://www.mandarichmodels.com/hot-pics/4.jpg", "http://www.mandarichmodels.com/hot-pics/5.jpg");
    imgView.setImageDrawable(drawable);

}

   private Drawable LoadImageFromWebOperations(String url, String string) {
      try
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        }catch (Exception e) {
            System.out.println("Exc="+e);
            return null;
        }
    }
}
Was it helpful?

Solution

Create an Array or List of the URLS you want to pull from, and then use that same code you have but put it into a loop over the length of the Array or List. And you should do this in a separate thread so that you don't generate an ANR. Look up AsyncTask.

List<String> urls;
for(int i=0; i<urls.size(); i++) {
    Drawable d = LoadImageFromWebOperations(urls.get(i));
    // do something interesting
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top