91

I want to use the Glide Android library to download an image and show in ImageView.

In the previous version we used:

Glide.with(mContext).load(imgUrl)
                .thumbnail(0.5f)
                .placeholder(R.drawable.PLACEHOLDER_IMAGE_NAME)
                .error(R.drawable.ERROR_IMAGE_NAME)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);

But I have seen Glide documentation:

it says use GlideApp.with() instead Glide.with()

My concern is a missing placeholder, error, GlideApp, and other options.

I am using

 compile 'com.github.bumptech.glide:glide:4.0.0'

Where am I doing wrong? With reference to here.

How has GlideApp.with() been used?

The API is generated in the same package as the AppGlideModule and is named GlideApp by default. Applications can use the API by starting all loads with GlideApp.with() instead of Glide.with():

GlideApp.with(fragment)
   .load(myUrl)
   .placeholder(placeholder)
   .fitCenter()
   .into(imageView);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ritesh Bhavsar
  • 1,343
  • 1
  • 9
  • 19

8 Answers8

231

Try using RequestOptions:

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_placeholder);
requestOptions.error(R.drawable.ic_error);

Glide.with(context)
     .setDefaultRequestOptions(requestOptions)
     .load(url).into(holder.imageView);

EDIT

If .setDefaultRequestOptions(requestOptions) does not work, use .apply(requestOptions):

Glide.with(MainActivity.this)
            .load(url)
            .apply(requestOptions)
            .into(imageview);
 // or this
 Glide.with(MainActivity.this)
            .load(url)
            .apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
            .into(imageview);

 // or this
 Glide.with(MainActivity.this)
            .load(url)
            .apply(RequestOptions.placeholderOf(R.drawable.booked_circle).error(R.drawable.))
            .into(imageview);

EDIT 2 Bonus

Here are some other changes in Glide-4

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
34

If you use Glide package dependencies, compile 'com.github.bumptech.glide:glide:3.7.0', then use should be to use the below code:

GlideApp
    .with(your context)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_image)
    .error(R.drawable.error_image)
    .into(myImageView);

Note: As in the documentation,

Round Pictures: CircleImageView/CircularImageView/RoundedImageView are known to have issues with TransitionDrawable (.crossFade() with .thumbnail() or .placeholder()) and animated GIFs, use a BitmapTransformation (.circleCrop() will be available in v4) or .dontAnimate() to fix the issue.

The latest updated version compile com.github.bumptech.glide:glide:4.1.1 then use should be to use the below code:

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.default_avatar)
                    .error(R.drawable.default_avatar)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.HIGH)
                    .dontAnimate()
                    .dontTransform();

Glide.with(this)
     .load(url)
     .apply(options)
     .into(imageView);

See the latest version of glide, bug fixes and features.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ND1010_
  • 3,743
  • 24
  • 41
  • glide is such weird library to use they haven't updated on github – Anand Savjani Sep 22 '18 at 10:05
  • Ya @AnandSavjani they didn't updated in GitHub but you can find their updates into this link https://github.com/bumptech/glide/releases – ND1010_ Sep 24 '18 at 06:12
  • 1
    If you don't like glide then you can create your own way to load images ,gif that provide also Caching and you can freely upload it into github and do always updated – ND1010_ Sep 24 '18 at 06:16
11

If you want to use GlideApp you have to add to dependencies annotation processor like on the screenshot:

How to add GlideApp to your project

Then include an AppGlideModule implementation in your application:

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}

Do not forget about the @GlideModule annotation. Then you need to Build project. And GlideApp will be automatically generated.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vlad Pylyp
  • 323
  • 3
  • 9
7

We have no need to use RequestOptions also.

The generated API adds a GlideApp class, that provides access to RequestBuilder and RequestOptions subclasses. The RequestOptions subclass contains all methods in RequestOptions and any methods defined in GlideExtensions. The RequestBuilder subclass provides access to all methods in the generated RequestOptions subclass without having to use apply:

Using Glide :-

A request without the generated API might look like this:

Glide.with(fragment)
    .load(url)
    .apply(centerCropTransform()
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.error)
        .priority(Priority.HIGH))
    .into(imageView);

Using GlideApp :-

With the generated API, the RequestOptions calls can be inlined:

GlideApp.with(fragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.error)
    .priority(Priority.HIGH)
    .into(imageView);

You can still use the generated RequestOptions subclass to apply the same set of options to multiple loads, but generated RequestBuilder subclass may be more convenient in most cases.

Shubham Sejpal
  • 3,556
  • 2
  • 14
  • 31
6

Dependencies:

compile 'com.github.bumptech.glide:glide:4.1.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1'

Add an appropriately annotated AppGlideModule implementation:

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public final class MyAppGlideModule extends AppGlideModule{}

In addition, if you have used the jack option, in order to avoid the following similar errors, you need use Android Studio 3.0.0 preview.

Error:Execution failed for task ':app:transformJackWithJackForDebug'. com.android.jack.ir.JNodeInternalError: java.lang.Exception: java.lang.AssertionError: No yet implemented

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zhen She
  • 129
  • 1
  • 9
4

Working

Glide.with(context!!)
     .load(user.profileImage)
     .apply (RequestOptions.placeholderOf(R.drawable.dummy_user))
     .into(edit_profile_image)
Gurwinder Singh
  • 109
  • 1
  • 3
2

If you want to use a common placeholder everywhere in your app then you can do it like this way:

As we are creating GlideModule from Glide v4, you can copy/paste this class in your project so you will able to use GlideApp class (for more steps - follow this):

@GlideModule
public class SampleGlideModule extends AppGlideModule {
    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
        builder.setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.logo).error(R.drawable.logo));
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
    }
}

You can give all the request options here to set as default.

By creating this class you do not need to use .placeholder with GlideApp, it will be applied automatically.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
1
RequestOptions options = new RequestOptions()
            .placeholder(R.drawable.null_image_profile)
            .error(R.drawable.null_image_profile);
    //.centerCrop()
    //.diskCacheStrategy(DiskCacheStrategy.ALL)
    //.priority(Priority.HIGH);

    Glide.with(context).load(imageUrl)
            .apply(options)
            .into(profileImage);
Trk
  • 95
  • 1
  • 12