CHUVASH.eu

CHunky Universe of Vigouros Astonishing SHarepoint :)

Android: Asynchronously download images

In the Malmöfestivalen for Android where I participate we wanted to show thumbnails of events in lists. We have urls of thumbnails from Malmofestivalen API. This information is stored in a sqlite database and a SimpleCursorAdapter is used for getting the events from the database. It is possible to bind the urls to ImageView objects (by downloading the stream: 1, 2, 3). The problem is in a listactivity with many images it will freeze. So it must be an AsyncTask, and it should be some kind of local cache to avoid loading same images over and over again. Fortunately there is already ImageDownloader class provided in the android blog post: Multithreading For Performance .

The next step is to create custom adapter and extend from SimpleCursorAdapter: EventCursorAdapter, like described on Android-er and and SP-Technolab:

public class EventCursorAdapter extends SimpleCursorAdapter {
	private final ImageDownloader imageDownloader = new ImageDownloader();
	public EventCursorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {		
		super(context, layout, c, from, to);
		imageDownloader.setMode(ImageDownloader.Mode.NO_DOWNLOADED_DRAWABLE);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = super.getView(position, convertView, parent);
		
		Cursor cursor = getCursor();
		cursor.moveToPosition(position);

		ImageView iv = (ImageView) row.findViewById(R.id.eventitemrowimage);

		String url = cursor.getString(cursor.getColumnIndex(EventProvider.EVENT_KEY_URISMALLIIMAGE));
		if (url.length() > 0) {			
			imageDownloader.download(url, iv);
		}
		
		
		String start = cursor.getString(cursor.getColumnIndex(EventProvider.EVENT_KEY_STARTDATE));
		String end = cursor.getString(cursor.getColumnIndex(EventProvider.EVENT_KEY_ENDDATE));
		String dateString = DateHelper.createShortDateResume(start, end);
		TextView tv = (TextView) row.findViewById(R.id.eventitemrowtimeresume);
		tv.setText(dateString);
		return row;
	}
}

In this code the view is created by the super class and then it is modified, the alternative is to “inflate the view” completely self.

The result is an event list which opens directly and loads thumbnails asynchronously:

listactivity with images

 

About these ads

4 responses to “Android: Asynchronously download images

  1. Justin Cooney 2012-04-26 at 16:32

    A very interesting example of how to download images asynchronously!

    • Anatoly Mironov 2012-04-26 at 16:58

      Thank you Justin! Glad you liked it. I think async is the only option we have nowadays :)

  2. matias elorriaga 2014-04-07 at 12:32

    this is an excellent article, but I have one question.. how do we handle device orientation changes? every time this happens, the listview re-download the images..

    • Anatoly Mironov 2014-04-07 at 12:35

      Good question. I have not thought about it. The view I had had only the vertical orientation. What happens on orientation change? Gets the view redrawed then?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Discovering SharePoint

And having fun doing it

Bram de Jager's SharePoint blog

My view and thoughts on SharePoint.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

The Zuul Cat Idea Brewery

Where ideas on software development and entrepreneurship brew.

Paul J. Swider

Inspire! Teach! Awe!

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Alexander Ahrens

MCPD | SharePoint | Web Development | JavaScript | .NET

Cameron Dwyer | SharePoint, Outlook, OnePlaceMail

OnePlaceMail, SharePoint, Outlook & Life's Other Little Wonders

.:paul.tavares:.

Me and My doings!

Share SharePoint Points

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

SPJoel

SharePoint for everyone

SharePointRyan.com

Ryan Dennis is a SharePoint Solutions Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode, ya mama

Treacle tarts for great justice

Follow

Get every new post delivered to your Inbox.

Join 224 other followers

%d bloggers like this: