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

  • SEARCH
  • Home
  • 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 7007161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:34:18+00:00 2026-05-27T21:34:18+00:00

import java.lang.ref.SoftReference; import java.util.HashMap; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; public class AsyncImageLoader {

  • 0
import java.lang.ref.SoftReference;
import java.util.HashMap;

 import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;


public class AsyncImageLoader {
private HashMap<String, SoftReference<Drawable>> imageCache;
HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String,    SoftReference<Drawable>>();


public AsyncImageLoader() {
    //HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String, SoftReference<Drawable>>();
}

public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {

    if (drawableMap.containsKey(imageUrl)) {
        SoftReference<Drawable> softReference = imageCache.get(imageUrl);
        Drawable drawable = softReference.get();
        if (drawable != null) {
            return drawable;
        }
    }
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
        }
    };

    //this is the new thread that download the image from url
    new Thread() {
        @Override
        public void run() {
            Drawable drawable = loadImageFromUrl(imageUrl);
            imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
            Message message = handler.obtainMessage(0, drawable);
            handler.sendMessage(message);
        }
    }.start();
    return null;
}

public static Drawable loadImageFromUrl(String url) {
    return null;
    // ...
}

public interface ImageCallback {
    public void imageLoaded(Drawable imageDrawable, String imageUrl);
}

}

OK, My understanding is such:
1)Check to see if the image in the cache, if it is then return drawable;
2)If not, then create a new handler to send a message to UI thread with Drawable as object, and this handler won’t send until the imageloaded callback function is called
3)create a new thread to initiate download image from url.

4)
specifically the sequence for handler happens in this order
A)Message message = handler.obtainMessage(0, drawable);
B) public void handleMessage(Message message) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}
C) handler.sendMessage(message);

The issue I have is Number 4, I am abit unclear, obtainmessage(0, drawable)? obtain from where? where’s the source? How do I know where it come from, UI thread or others? So, once obtained that message, the message handler would do the callback. public void handleMessage(Message message) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}

finally it send the message with drawable as payload. handler.sendMessage(message);
but how do I know where its sending? Does it always have UI thread as final destination?

thanks

  • 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-05-27T21:34:19+00:00Added an answer on May 27, 2026 at 9:34 pm

    basically — there are two threads here, the main UI thread that most of this code is executed in, and the download thread defined at the bottom.

    the obtainMessage call simply returns a message object from a pool, which is cheaper than instantiating a new one. It’s analogous to a constructor in this usage.

    since the handler is defined in the UI thread, the handleMessage method is executed in the UI thread as well. The message itself is simply a way of getting the drawable from the download thread to the UI thread, as well as trigger the callback, when the download is complete.

    So basically all this code is doing is: if the drawable exists in the (SoftReference) cache, then return it. Otherwise, get a reference to a handler and start a thread which downloads the drawable. When that thread is done downloading, it creates a message and sends it to the handler, which in turns calls imageCallback.imageLoaded and passes the newly downloaded drawable.

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

Sidebar

Related Questions

Imagine I have the following situation: Test1.java import java.lang.ref.WeakReference; public class Test1 { public
Here's Pair.java import java.lang.*; import java.util.*; public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB>
import java.lang.reflect.*; import java.util.Arrays; class Test { class Inner { public <T> Inner(T arg)
import java.util.Scanner; import java.lang.String; public class Test { public static void main(String[] args) {
import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.NoSuchElementException; import javax.swing.JOptionPane; public class
import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.NoSuchElementException; import javax.swing.JOptionPane; public class
I have a problem import java.math.BigInteger; import java.io.*; import java.util.*; import java.lang.*; public class
Demo code: import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; public class Main { public static void main(String[]
import java.util.HashMap; import java.io.*; import java.util.*; public class Fegan implements Comparable{ HashMap<String, Integer> cart
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }

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.