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);
}
}

New Topic/Question
This topic is locked


MultiQuote







|