SoFunction
Updated on 2025-04-10

Android asynchronously loading image instance code

The main process of asynchronously loading pictures is to determine whether there is an image in the cache. If it exists, it will be returned directly. If it does not exist, it will be downloaded and cached.

The following is to create an asynchronous download class:

Copy the codeThe code is as follows:

/**
 * User: Tom
 * Date: 13-5-13
* Time: 8:07 pm
 */
public class AsnycImageLoader {

//Define a HashMap to store cached Image key as String Value as a resource file with a weak reference
// Pictures To facilitate the recycling of JAVA
    private Map<String, SoftReference<Drawable>> imageCache = null;
    public AsnycImageLoader() {
        imageCache = new HashMap<String, SoftReference<Drawable>>();
    }

    /**
* Loading pictures
* <p>imageurl is the URL to download the resource.
* ImageCallback When there is no related image in the cache, it will be downloaded online in a timely manner.
* Use Handler to operate the UI under multi-threading. Use the callback interface to update the UI
     * </p>
     * @param imageUrl
     * @param callback
     * @return
     */
    public Drawable loadDrawable(final String imageUrl, final ImageCallback callback) {
//Document whether there is a cached image in ImageCache
        if ((imageUrl)) {
            SoftReference<Drawable> softReference = (imageUrl);
            if (() != null) {
                return ();
            }
        }
//Define the Handler for the operation UI
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                ((Drawable) );
            }
        };

        new Thread(new Runnable() {
            @Override
            public void run() {
                Drawable drawable = loadImageFromUrl(imageUrl);
                (imageUrl, new SoftReference<Drawable>(drawable));
                Message message = (0, drawable);
                (message);
            }
        }).start();
        return null;
    }

//Get resources based on the URL address
    protected Drawable loadImageFromUrl(String imageUrl) {
        try {
            return (new URL(imageUrl).openStream(), "src");
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

//Callback interface
    public interface ImageCallback {
        public abstract void imageLoaded(Drawable drawable);
    }
}

Main Activity:

Copy the codeThe code is as follows:

/**
 * User: Tom
 * Date: 13-5-13
* Time: 8:33 pm
 */
public class LoadImage extends Activity {
    private AsnycImageLoader loader = null;

    public void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView();

        loader = new AsnycImageLoader();

        loadImage("https:///images/", .image1);
        loadImage("https:///images/", .image2);
        loadImage("http://pic28./20130421/12302174_231210305323_2.jpg", .image3);


    }

    public void loadImage(String url, int id) {
        final ImageView imageView = (ImageView) findViewById(id);
        Drawable cacheImage = (url, new () {
            @Override
            public void imageLoaded(Drawable drawable) {
                (drawable);
            }
        });
        if (cacheImage != null) {
            (cacheImage);
        }
    }
}