1 Replies - 442 Views - Last Post: 28 October 2012 - 08:58 PM

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

How to download and display multiple images?

Posted 28 October 2012 - 08:57 PM

I am trying to download and display multiple images, i unfortunately missed the class where we went over this so i dont really understand it too much. Could someone explain to me how i can modify this code to make it accept multiple images? Doesnt have to be all at once, currently the code works fine but it overwrites the first image so it only displays 1 image at a time.

Here is the code.

// This section sends the url for the image from a textbox to the second section of code, that much i understand.
private void onclickBtnStart(View v) {
    	Downloader downloader = new Downloader(imgLoaded);
downloader.execute(txtUrlBox.getText().toString());
    }


// This is the second section of code which displays it, i dont understand this quite so well.
package com.sean;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.*;
import java.io.*;
import java.net.*;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Downloader extends AsyncTask<String, Void, Bitmap> {

	private ImageView imgLoaded;
	
	// force user to pass in the ImageView object
	public Downloader(ImageView target) {
		imgLoaded = target;
	}
	
	@Override
	protected Bitmap doInBackground(String... urls) {
		// create bitmap variable
		Bitmap bitmap = null;
		
		// code in here runs on its own thread - return is received by onPostExecute()
		try {
			// construct URL object to target image to download
			URL url = new URL(urls[0]);
			// create connection via URL object
			URLConnection urlConnection = url.openConnection();
			// download the image
			bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream());
		} catch (MalformedURLException e) {
			Log.d("sean", e.getMessage());
		} catch (IOException e) {
			Log.d("sean", e.getMessage());
		} catch (Exception e) {
			Log.d("sean", "!!! Exception ", e);
		}
		
		return bitmap;
	}
	
	@Override
	protected void onPostExecute(Bitmap result) {
		// code in here runs on the UI thread
		imgLoaded.setImageBitmap(result);
	}
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to download and display multiple images?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10808
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: How to download and display multiple images?

Posted 28 October 2012 - 08:58 PM

Do NOT open duplicate threads. Topic closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1