I\'m a newbie programmer an I\'m making an android program that displays an image on ImageView from a given url. My problem is how do you use this on the AsyncTask?
Thes
based on the answer from comeGetSome i created my own implementation which works with normal ImageView class instead of creating a new class UrlImageView, also new options are provided like - what to do when the loading completes or cancels
Now load image as you want, like this from any of the three loadImage methods
UrlImageLoader urlImageLoader=new UrlImageLoader();
ImageView imageView=new ImageView(context);
//load and set the image to ImageView
urlImageLoader.loadImage(imageView, "http://www......com/.....jpg");
//for loading a image only - load image and do any thing with the bitmap
urlImageLoader.loadImage("http://www......com/.....jpg", new UrlImageLoader.OnLoadingCompleteListener() {
@Override
public void onComplete(ImageView imageView, Bitmap bmp) {
// do anything with the Bitmap
// here imageView will be null
}
@Override
public void onCancel(ImageView imageView) {
}
});
urlImageLoader.loadImage(imageView, "http://www......com/.....jpg", new UrlImageLoader.OnLoadingCompleteListener() {
@Override
public void onComplete(ImageView imageView, Bitmap bmp) {
// do anything with the Bitmap
// here imageView is not null
imageView.setImageBitmap(bmp);
}
@Override
public void onCancel(ImageView imageView) {
}
});
create this class for loading UrlImageLoader
/*special thanks to stackoverflow.com user comeGetSome for UrlImageLoadingTask code
* question - http://stackoverflow.com/questions/14332296/how-to-set-image-from-url-using-asynctask/15797963
* comeGetSome - http://stackoverflow.com/users/1005652/comegetsome
*/
package com.GameG.SealTheBox;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
public class UrlImageLoader {
public static interface OnLoadingCompleteListener {
public void onComplete(ImageView imageView, Bitmap bmp);
public void onCancel(ImageView imageView);
}
ArrayList<UrlImageLoadingTask> loadingList=new ArrayList<UrlImageLoadingTask>();
/**
* Loads a image from url and calls onComplete() when finished<br>
* @Note you should manually set the loaded image to ImageView in the onComplete()
* @param imageView
* @param url
* @param onComplete
*/
public void loadImage(ImageView imageView, String url, OnLoadingCompleteListener onComplete){
try {
URL url2=new URL(url);
if(imageView!=null){
for(int i=0;i<loadingList.size();i++){
UrlImageLoadingTask tmptask=loadingList.get(i);
if(tmptask.updateView!=null && tmptask.updateView.equals(imageView)){
tmptask.cancel(true);
break;
}
}
}
UrlImageLoadingTask loadtask=new UrlImageLoadingTask(imageView,onComplete,url);
loadtask.execute(url2);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
/**
* Loads a image from url and calls onComplete() when finished
* @param url
* @param onComplete
*/
public void loadImage(String url, OnLoadingCompleteListener onComplete){
loadImage(null,url,onComplete);
}
/**
* Loads a image from url and sets the loaded image to ImageView
* @param imageView
* @param url
*/
public void loadImage(ImageView imageView, String url){
loadImage(imageView,url,null);
}
/**
* Cancel loading of a ImageView
*/
public void cancel(ImageView imageView){
for(int i=0;i<loadingList.size();i++){
UrlImageLoadingTask tmptask=loadingList.get(i);
if(tmptask.updateView.equals(imageView)){
loadingList.remove(i);
tmptask.cancel(true);
break;
}
}
}
/**
* Cancel loading of a Url
*/
public void cancel(String url){
for(int i=0;i<loadingList.size();i++){
UrlImageLoadingTask tmptask=loadingList.get(i);
if(tmptask.url.equals(url)){
loadingList.remove(i);
tmptask.cancel(true);
break;
}
}
}
/**
* Cancel all loading tasks
*/
public void cancelAll(){
while(loadingList.size()>0){
UrlImageLoadingTask tmptask=loadingList.get(0);
loadingList.remove(tmptask);
tmptask.cancel(true);
}
}
private class UrlImageLoadingTask extends AsyncTask<URL, Void, Bitmap> {
public ImageView updateView=null;
public String url;
private boolean isCancelled = false;
private InputStream urlInputStream;
private OnLoadingCompleteListener onComplete=null;
private UrlImageLoadingTask(ImageView updateView, OnLoadingCompleteListener onComplete, String url) {
this.updateView=updateView;
this.onComplete=onComplete;
this.url=url;
}
@Override
protected Bitmap doInBackground(URL... params) {
try {
URLConnection con = params[0].openConnection();
// can use some more params, i.e. caching directory etc
con.setUseCaches(true);
this.urlInputStream = con.getInputStream();
return BitmapFactory.decodeStream(urlInputStream);
} catch (IOException e) {
Log.w(UrlImageView.class.getName(), "failed to load image from " + params[0], e);
return null;
} finally {
if (this.urlInputStream != null) {
try {
this.urlInputStream.close();
} catch (IOException e) {
; // swallow
} finally {
this.urlInputStream = null;
}
}
}
}
@Override
protected void onPostExecute(Bitmap result) {
if (!this.isCancelled) {
// hope that call is thread-safe
if(onComplete==null){
if(updateView!=null)
this.updateView.setImageBitmap(result);
}else{
onComplete.onComplete(updateView, result);
}
}
loadingList.remove(this);
}
/*
* just remember that we were cancelled, no synchronization necessary
*/
@Override
protected void onCancelled() {
this.isCancelled = true;
try {
if (this.urlInputStream != null) {
try {
this.urlInputStream.close();
} catch (IOException e) {
;// swallow
} finally {
this.urlInputStream = null;
}
}
} finally {
super.onCancelled();
if(onComplete!=null)
onComplete.onCancel(updateView);
loadingList.remove(this);
}
}
}
}