Complete Guide to Downloading Images to Bitmap Using Glide

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: Glide | Bitmap Download | Android Image Processing

Abstract: This article provides a comprehensive guide on using the Glide library to download images into Bitmap objects, covering the latest API usage, CustomTarget implementation, size control strategies, and backward compatibility. By comparing different methods' pros and cons, it helps developers choose the most suitable solution with complete code examples and best practices.

Introduction

In Android development, Glide is a powerful image loading library widely used for loading images from networks or local storage into ImageViews. However, in certain scenarios, developers need to download images directly into Bitmap objects for further processing rather than displaying them directly in ImageViews. This article delves into how to achieve this using Glide.

Glide Basics Review

The basic usage of Glide typically involves loading images into ImageViews, for example:

Glide
   .with(context)
   .load(getIntent().getData())
   .placeholder(R.drawable.ic_loading)
   .centerCrop()
   .into(imageView);

This approach is simple and efficient but does not directly provide a Bitmap object. To download images into Bitmap, more advanced APIs are required.

Using CustomTarget to Download Bitmap

In Glide 4.10.0 and above, using CustomTarget is recommended for downloading images into Bitmap. This method offers better resource management, avoiding crashes due to lifecycle issues.

Kotlin Implementation

Glide.with(this)
        .asBitmap()
        .load(imagePath)
        .into(object : CustomTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                imageView.setImageBitmap(resource)
            }
            override fun onLoadCleared(placeholder: Drawable?) {
                // Called when the ImageView is cleared
                // If the bitmap is referenced elsewhere, clear it here
            }
        })

Java Implementation

Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
            }
        });

Bitmap Size Control

CustomTarget allows developers to specify the size of the Bitmap. If no size is specified, the original image dimensions are used. For specific sizes, specify them in the CustomTarget constructor:

into(object : CustomTarget<Bitmap>(1980, 1080)

This approach provides better control over memory usage and performance.

Backward Compatibility Handling

Glide 4.8.0 and Below

Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }
        });

Glide 3.7.0 and Below

Glide.with(this)
        .load(path)
        .asBitmap()
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                imageView.setImageBitmap(resource);
            }
        });

SimpleTarget Deprecation Note

It is important to note that SimpleTarget has been deprecated. The main reason is that it does not enforce developers to stop using loaded resources after the target is cleared, which can lead to crashes and graphical corruption. Although SimpleTarget can still be used, developers must ensure that the associated Bitmap is not used after the ImageView is cleared.

Alternative Methods Analysis

Besides the CustomTarget method, other approaches exist but have their own pros and cons:

FutureTarget Method

Bitmap theBitmap = Glide
        .with(this)
        .load("http://....")
        .asBitmap()
        .into(100, 100)
        .get();

This method returns a FutureTarget<Bitmap> and requires handling ExecutionException and InterruptedException with try-catch blocks. While it directly provides a Bitmap, using it on the main thread may cause ANR.

submit().get() Method

bitmap = Glide.with(c).asBitmap().load("url").submit().get();

This is another way to directly obtain a Bitmap, but thread safety must be considered.

Best Practices Recommendations

1. Always use the latest version of Glide for optimal performance and security

2. Perform time-consuming image downloads in non-UI threads

3. Control Bitmap sizes appropriately to avoid memory overflow

4. Promptly clear references to Bitmaps that are no longer used

5. Use CustomTarget instead of the deprecated SimpleTarget

Conclusion

Through this article, developers can fully understand how to use Glide to download images into Bitmap objects. CustomTarget is the most recommended solution, offering good resource management and lifecycle support. When choosing a specific implementation method, consider the application's specific needs and performance requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.