0 Replies - 4270 Views - Last Post: 02 October 2012 - 05:53 PM

#1 H3R3T1C   User is offline

  • Android Expert
  • member icon

Reputation: 278
  • View blog
  • Posts: 757
  • Joined: 30-March 07

[Android] Asynchronous Image Loader

Posted 02 October 2012 - 05:53 PM

Description: URLImage img = new URLImage("www.website.com/image.jpg","image.jpg");
new SingleImageLoaderAsync(img,iv,new SingleImageLoaderAsync.ImageLoaderListener{
public void onload(boolean didImageLoad,boolean isCancelled){
// your code
}
}).execute();This code snippet is used to load image thumbnails asynchronous from a url.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;


public class SingleImageLoaderAsync extends AsyncTask<Void,Boolean,Void>{
	
	public interface ImageLoaderListener
	{
		public void onload(boolean didImageLoad,boolean isCancelled);// didImageLoad returns true if image loaded false otherwise
	}
	
	public boolean isRunning;
	private static final String cacheDir = "/sdcard/cache/";
	private ImageLoaderListener listener;
	private ImageView iv;
	private URLImage img;
	
	public SingleImageLoaderAsync(URLImage img,ImageView iv ,ImageLoaderListener l)
	{
		this.iv = iv;
		this.img = img;
		listener = l;
	}
	
	@Override
	protected void onprogressUpdate(Boolean... values) {
		
		listener.onload(values[0],this.isCancelled());
		if(this.isCancelled()) return;
                iv.setImageBitmap(BitmapFactory.decodeFile(cacheDir+img.name));
		iv.invalidate();
	}
	
	@Override
	protected void onPostExecute(String result) {
		isRunning = false;
		listener.finishLoad(isCancelled());
	}

	@Override
	protected String doInBackground(Void... params) {
		isRunning = true;
		crateFolders();
		this.publishProgress(download(cacheDir+img.name,img.url));
		return null;
	}
	private boolean download(String path,String u)
	{
		
		if(new File(path).exists())
			return true;
		try{
			URL url = new URL(u); 
	        URLConnection connection = url.openConnection();
	        connection.connect();

	        InputStream input = new BufferedInputStream(url.openStream());
	        OutputStream output = new FileOutputStream(path);
	
	        byte data[] = new byte[1024];
	        int count;
	        while ((count = input.read(data)) != -1) {
	            output.write(data, 0, count);
	        }
	
	        output.flush();
	        output.close();
	        input.close();
	    } catch (Exception e) {
	    	e.printStackTrace();
	    	Log.d("ImageLoaderAsync", e.getLocalizedMessage());
	    	return false;
	    }
		return true;
	}
	private void crateFolders()
	{
		File f = new File(cacheDir);
		if(!f.exists())
			f.mkdirs();
	}
	
	public class URLImage{
		public String url; // url of image file
		public String name; // name of image | ex. image.jpg
		
		public URLImage(String u,String n)
		{
			url = u;
			name = n;
		}
	
	}

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1