Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Adding Image from URL, BlogReader APP

Hello,

As i completed the BlogReader app, and works perfectly, i even managed to use my own website, and added a different layout so it can also support an image.

However, from the json file, there is a JSONObject, that contains the URL of the image. How can i add the image of each post asynchronously without having to wait for them or the application crashing.

I've added to the ArrayList<HashMap> we did a thumbnail attribute. It's a string that contains the whole URL.

I also linked it to R.id.icon String[] keys = { KEY_TITLE, KEY_EXCERPT, KEY_THUMBNAIL}; int[] ids = { R.id.title, R.id.secondLine, R.id.icon};

I did some research and the best way to do is using the AsyncTask, however should i create another one just for the image, or do i use the same one we did in the tutorial.

Thanks, Ayman

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Ayman,

The best way I've found to download images asynchronously is to use a 3rd party library called SmartImageView. I actually wrote a blog post about it a while back.

The basic idea is that you would load a default image from your project for each row to begin with. Then you could fill the SmartImageView with the URL you retrieved. That's the tricky part, though: you will need to loop through the items in your list. You could write a separate loop and download all the images asynchronously once your adapter is loaded, or you could create a custom adapter and download each image as each row of the list is drawn.

Let us know if this is enough to get you started or if you have other questions!

Yea i saw your blog post, from another forum reply, and it did come in handy.

For now i will just create a custom adapter, and in that i'll have an AsyncTask in which it will download the images while loading. I'll let you know of my progress once i achieve it.

Thanks again, Ayman