MR Mido
MR Mido

Reputation: 1688

Async Image Download and storing in an array of ImageView

I have been searching for a while now for a way of doing an Async download of multiple images and storing them in an array. I have some GREAT example of this being used on Lists but I would like just to have a way of downloading these and use them later on. Thanks for reading. Any suggestion would be greatly appreciated.

Upvotes: 2

Views: 6303

Answers (2)

user3274243
user3274243

Reputation: 1

you need to change new URL(imageURL) -> new URL(url)

and put return type, under the "e.printStackTrace();" (return null;)

I checked it worked.

Upvotes: 0

Kenny
Kenny

Reputation: 5542

Try something like this:

public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
     protected Bitmap doInBackground(String... urls) {
         return loadImageFromNetwork(urls[0]);
     }

     protected void onPostExecute(Bitmap result) {
         //Do something with bitmap eg:
         mImageView.setImageBitmap(result);
     }
 }

 private Bitmap loadImageFromNetwork(String url){
      try {
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
          return bitmap;
      } catch (Exception e) {
          e.printStackTrace();
    }
 }

Upvotes: 5

Related Questions