Glide — Advanced Loading

In the first tutorial, we've looked why you should use Glide and a simple example request to load an image from an Internet source. But this is not the only possible image source for Glide. Glide can also load images from the Android resources, files and URIs. In this tutorial, we'll cover all three options.

Glide Series Overview

Loading from Resources

First up is loading from Android resources. Instead of giving a String pointing to an Internet URL, you give a resource int.

Glide 4.x

int resourceId = R.mipmap.ic_launcher;

GlideApp  
    .with(context)
    .load(resourceId)
    .into(imageViewResource);

Glide 3.x

int resourceId = R.mipmap.ic_launcher;

Glide  
    .with(context)
    .load(resourceId)
    .into(imageViewResource);

If you're confused by the R.mipmap., it's Android's new way of handling icons.

Of course, you can set a resource directly by using the methods of the ImageView class. However, this can be interesting if you're using more advanced topics like dynamic transformations.

Loading from File

Second up is loading from a file. This can be useful when you let the user select a photo to display an image (similar to a gallery). The parameter is just a File object.

Glide 4.x

// this file probably does not exist on your device. However, you can use any file path, which points to an image file
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");

GlideApp  
    .with(context)
    .load(file)
    .into(imageViewFile);

Glide 3.x

// this file probably does not exist on your device. However, you can use any file path, which points to an image file
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");

Glide  
    .with(context)
    .load(file)
    .into(imageViewFile);

Loading from Uri

Lastly, you can also load images defined by an Uri. The request is no different from the previous options:

Glide 4.x

// this could be any URI. for demonstration purposes we're just creating an Uri pointing to a launcher icon
Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);

GlideApp  
    .with(context)
    .load(uri)
    .into(imageViewUri);

Glide 3.x

// this could be any URI. for demonstration purposes we're just creating an Uri pointing to a launcher icon
Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);

Glide  
    .with(context)
    .load(uri)
    .into(imageViewUri);

The small helper function is a simple conversion from the resourceId to an URI.

public static final String ANDROID_RESOURCE = "android.resource://";  
public static final String FOREWARD_SLASH = "/";

private static Uri resourceIdToUri(Context context, int resourceId) {  
    return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);
}

However, the URI does not have to be generated from a resourceId. It can be any valid URI.

Load Bitmap or Drawable

Starting with Glide 4.3.0, you can pass a Bitmap or Drawable object to the load() method.

Bitmap bm = ...

Glide  
    .with(context)
    .load(bm)
    .into(imageView);

Outlook

That's it for this tutorial. You've learned the basic loading principles. Next, we can finally look at more interesting stuff. In the next tutorial we'll cover adapter use and Glide's caching in ListViews and GridViews.

Explore the Library

Find interesting tutorials and solutions for your problems.