Glide: How to load Drawable?

Created on 20 Aug 2015  路  13Comments  路  Source: bumptech/glide

Hi everyone!

I use this code to load drawable(android Drawable class), but fail!

Glide.with(GroupChatMemberListActivity.this)
              .load(drawable)
              .into(holder.imageView);

Lot print

java.lang.IllegalArgumentException: Unknown type class android.graphics.drawable.Drawable. 
You must provide a Model of a type for which there is a registered ModelLoader,
 if you are using a custom model,
 you must first call Glide#register with a ModelLoaderFactory for your custom model class
            at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629)
            at com.bumptech.glide.RequestManager.from(RequestManager.java:621)

My model class is

public class MyGlideModule implements GlideModule {

    private static final int DISK_CACHE_SIZE = 365 * 1024 * 1024; // 365MB
    private static final String DISK_CACHE_NAME = "glide_disk_cache";

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        MemorySizeCalculator calculator = new MemorySizeCalculator(context);
        int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
        int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
        builder.setDiskCache(
                new InternalCacheDiskCacheFactory(context, DISK_CACHE_NAME, DISK_CACHE_SIZE))
                .setMemoryCache(new LruResourceCache((int) (defaultMemoryCacheSize * 1.5)));
        builder.setBitmapPool(new LruBitmapPool((int) (defaultBitmapPoolSize * 1.5)));
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        OkHttpClient client = new OkHttpClient();
        try {
            // only debug use
            Class c = Class.forName("com.facebook.stetho.okhttp.StethoInterceptor");
            client.networkInterceptors().add((Interceptor) c.newInstance());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
    }

}

What should I do for this?

Thanks!

question

Most helpful comment

because my view is listview, adapter must be use glide for all getView, prevent image show on wrong position.

now I use placeholder instead of load, it work fine.

Glide.with(GroupChatMemberListActivity.this)
             .load("")
             .placeholder(drawable)
             .into(holder.imageView);

All 13 comments

have you tried this?
Glide.with(--your activity--)
.fromResource(--your drawable--)
.into(--your container--);

i think load is used to load image from url *CMIIW

Glide had no such method fromResource(--your drawable--), only fromResource()

why using glide if the resource is in drawable? i think it will easier to use setImageDrawable than use glide

because my view is listview, adapter must be use glide for all getView, prevent image show on wrong position.

now I use placeholder instead of load, it work fine.

Glide.with(GroupChatMemberListActivity.this)
             .load("")
             .placeholder(drawable)
             .into(holder.imageView);

I close this issue now, thanks @yfsx

@yfsx is right, you should use .load(R.drawable.your_drawable).
If you replace Glide.with... with holder.ImageView.setImageDrawable(drawable) your images will show up in the correct places. However I think Glide defers drawable loading to a background thread so scrolling may be faster.

@TWiStErRob thanks for your reply.

I can't use .load(R.drawable.your_drawable), because our app have some custom theme, if I use R.drawable to load the drawable, the theme will not effective.
So I must use getDrawable(), now I use placeholder instead of load, it work fine.

I can't find a method named placeholder in Glide. I am using version "4.0.0-RC0"

Placeholders are now loaded using RequestOptions @kumarbanty .

RequestOptions requestOptions = new RequestOptions()
    .placeholder(resourceId);

Glide.with(context)
    .load("")
    .apply(requestOptions)
    .into(imageView);

@Sundin trying to load an empty string throws a fail message and doesn't load the placeholder, do you know how to do this on 4?

This is not ideal but in case placeholder does not work because of the reason mentioned above you can fallback to error.
Like so:

Glide.with(GroupChatMemberListActivity.this)
             .load("")
             .placeholder(drawable)
             .error(drawable)
             .into(holder.imageView);

I tried it and it works.

imagen
This works fine.
Glide.with(mContext) .asBitmap() .load(R.drawable.ic_launcher_background) .into(holder.photo);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

billy2271 picture billy2271  路  3Comments

Anton111111 picture Anton111111  路  3Comments

sant527 picture sant527  路  3Comments

kenneth2008 picture kenneth2008  路  3Comments

FooBarBacon picture FooBarBacon  路  3Comments