views:

118

answers:

1

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

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
}
BrennaSoft
thanks for the answer rpond im trying to initialize it and make it work do i need to go to the .xml and define <string-array, or just a regular array work?i think i may have put it in right section just not pics showing up now. dont fully know what im missing place the code surrounding IamgeView.
If the urls are static you can put it into a string-array in resources. If the urls will be dynamic then you will need to create a String[] array or List<String> to iterate over.
BrennaSoft
should i place this inside an arrays.xml then im totally lost on how to mix ur code with the one i have. do you know of any good tutorials for this along with making an option to slide sideways to go through the url's.