Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8006893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:39:38+00:00 2026-06-04T17:39:38+00:00

Hello fellow developers :) I’ve made a very basic image fetcher for Android to

  • 0

Hello fellow developers 🙂

I’ve made a very basic image fetcher for Android to download and display bitmaps from the web on my application the code for it is:

public class BitmapFetcher {

    private static HashMap<String, SoftReference<Bitmap>> bitmapCache = new HashMap<String, SoftReference<Bitmap>>();

    public static Bitmap fetchBitmap(String urlString) {
        SoftReference<Bitmap> cachedBitmap = bitmapCache.get(urlString);

        if (cachedBitmap != null && cachedBitmap.get() != null) {
            return cachedBitmap.get();
        }

        try {
            InputStream is = fetch(urlString);

            Bitmap bitmap = BitmapFactory.decodeStream(is);
            SoftReference<Bitmap> softReferencedBitmap = new SoftReference<Bitmap>(bitmap);
            bitmapCache.put(urlString, softReferencedBitmap);

            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void fetchBitmapAsync(final String urlString, final ImageView view) {
        final Handler handler = new Handler() {
            public void handleMessage(Message message) {
                AsyncImageContainer imageContainer = (AsyncImageContainer) message.obj;

                imageContainer.applyImageToView();
            }
        };

        BitmapTaskRunnable asyncImageFetcherTask = new BitmapTaskRunnable(view, urlString, handler);

        new Thread(asyncImageFetcherTask).start();
    }

    public static InputStream fetch(String urlString) throws MalformedURLException, IOException {
        Log.d("BitmapFetcher", "fetch: " + urlString);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(urlString);
        HttpResponse response = httpClient.execute(request);

        return response.getEntity().getContent();
    }

}

BitmapTaskRunnable.java:

public class BitmapTaskRunnable implements Runnable {

    private ImageView imageView;
    private String imageUrl;
    private Handler handler;

    public BitmapTaskRunnable() {
    }

    public BitmapTaskRunnable(ImageView imageView, String imageUrl, Handler handler) {
        setImageView(imageView);
        setImageUrl(imageUrl);
        setHandler(handler);
    }

    public void run() {
        Bitmap bitmap = BitmapFetcher.fetchBitmap(getImageUrl());

        AsyncImageContainer imageContainer = new AsyncImageContainer(getImageView(), bitmap);

        handler.sendMessage(handler.obtainMessage(0, imageContainer));
    }

    public ImageView getImageView() {
        return imageView;
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

}

AsyncImageContainer.java:

public class AsyncImageContainer {

    private ImageView imageView;
    private Bitmap bitmap;

    public AsyncImageContainer() {
    }

    public AsyncImageContainer(ImageView imageView, Bitmap bitmap) {
        setImageView(imageView);
        setBitmap(bitmap);
    }

    public void applyImageToView() {
        getImageView().setImageBitmap(getBitmap());
    }

    public ImageView getImageView() {
        return imageView;
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

}

As I said, it’s very basic, with a very basic caching and no throttling on threads being created (this is already planned to be done soon).

Well, I’m currently experiencing a weird behavior from this when I need to load images in a ListView, what I did is: I have some classes that extend ArrayAdapter and overrides getView to display my layout, whenever I need an image on it, I will do the following:

BitmapFetcher.fetchBitmapAsync(news.getChannelAvatar(), holder.channelAvatarView);

And this should start make BitmapFetcher start a new thread which will download the bitmap and send a message to the handler to it apply the image to the view (as only the thread which created a view hierarchy can modify it).

Everything is fine for the 2nd to n-th ImageViews in the list, but the 1st result ALWAYS goes crazy, changing to images which have been downloaded until all the images are loaded and it settles to it. Then if I drag the list a little until the 1st result disappears and goes back to the top, it displays the correct image.

This is really bugging me, as I had done a much simpler version of the code (one which the handler handled directly placing the Bitmap in the ImageView, the Runnable did not exist, it was a simple anonymous Thread object with run() overriden, etc) and tried this version thinking that somehow fetchBitmapAsync was losing reference to the correct ImageView or something like that.

Does this happen for some thing that Android does to recycle Views inside a ListView? If not, what could be the reason? Am I being silly somewhere and after a couple days working on this code I’ve gone blind? 🙁

Thanks for all the help 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:39:39+00:00Added an answer on June 4, 2026 at 5:39 pm

    For all of my lazy image loading I use Prime. It even has an example of how to use it within a ListView.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello fellow developers. I have previously made a facebook fan page with an iframe
hello fellow java developers. I'm having a very strange issue. I'm trying to read
Hello fellow developers, I am trying to achieve something in Android and I would
Hello fellow developers, I am a total newbie in Android development and I am
Hello fellow developers! I am trying to get the build error codes from a
Hello fellow stackoverflow members! I'm very new to the C# language transfer from Java,
hello fellow java developers. I'm having a bit of an issue here. I have
Hello fellow software developers. I want to distribute a C program which is scriptable
Hello fellow developers. First of all I apologize beforehand for the wall of text
Hello fellow Grails Developers! I was wondering if you could help me with what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.