3

In Picasso there exists the RequestHandler class. And I can add custom RequestHandlers to Picasso.

How can this be done in Glide?

I for example want that following URI can be handled by a custom RequestHandler: "appicon:custom_data_to_interprete_manually"

EDIT - what I have so far

    public class GlideConfiguration implements GlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // Apply options to the builder here.
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        glide.register(CustomModelParams.class, CustomModelParams.class, new CustomFactory());
    }

    class CustomModelParams
    {
        final String data;

        public CustomModelParams(String data)
        {
            this.data = data;
        }

        public String getId()
        {
            return data;
        }
    }

    class CustomFactory implements ModelLoaderFactory<CustomModelParams, CustomModelParams>
    {
        @Override
        public ModelLoader<CustomModelParams, CustomModelParams> build(Context context, GenericLoaderFactory loaderFactory) {
            return new CustomModelLoader();
        }

        @Override
        public void teardown() {
        }
    }

    class CustomModelLoader implements ModelLoader<CustomModelParams, CustomModelParams>
    {
        public CustomModelLoader() {
            super();
        }

        @Override
        public DataFetcher<CustomModelParams> getResourceFetcher(final CustomModelParams model, int width, int height)
        {
            return new DataFetcher<CustomModelParams>()
            {
                @Override
                public CustomModelParams loadData(Priority priority) throws Exception { return model; }
                @Override
                public void cleanup() { }
                @Override
                public String getId() { return model.getId(); }
                @Override
                public void cancel() { }
            };
        }
    }

    class CustomBitmapDecoder implements ResourceDecoder<CustomModelParams, Bitmap>
    {
        private final Context context;

        public CustomBitmapDecoder(Context context)
        {
            this.context = context;
        }

        @Override
        public Resource<Bitmap> decode(CustomModelParams source, int width, int height) throws IOException
        {
            BitmapPool pool = Glide.get(context).getBitmapPool();
            Bitmap bitmap = pool.getDirty(width, height, Bitmap.Config.ARGB_8888);
            if (bitmap == null) {
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            }

            // TODO
            // create custom bitmap from CustomModelParams!!!

            return BitmapResource.obtain(bitmap, pool);
        }

        @Override
        public String getId()
        {
            return CustomBitmapDecoder.class.getName();
        }
    }
}

QUESTION

  • How do I link those classes together? The Decoder must somehow be linked with the new Model
  • How do I define, that my custom loader can handle a request? I have to somehow determine if the url I get can be handled by this loader...
prom85
  • 16,896
  • 17
  • 122
  • 242
  • Possible duplicate of [Using Glide to load bitmap into ImageView](http://stackoverflow.com/questions/31867322/using-glide-to-load-bitmap-into-imageview) – TWiStErRob Oct 01 '15 at 10:03
  • Did you solve this? I found it really hard to convert from RequestHandler to Glide's Loader as well – Hoa Vu Oct 07 '15 at 21:32
  • I'm sorry, no... I did not find out how to load special images automatically... – prom85 Oct 08 '15 at 04:49

2 Answers2

0

You can use ModelLoaders. See the Downloading custom sizes wiki for an example of a custom ModelLoader. Note that instead of a BaseGlideUrlLoader, you will want to register a ModelLoader that handles your particular data model.

Sam Judd
  • 7,317
  • 1
  • 38
  • 38
  • I tried creating something based on that information. Can you look at it and help me to get this working? I edited my main question. Thanks – prom85 Sep 11 '15 at 09:07
0

I have implemented very simular code for my project (It loads other applications icons from Package Manager) and

@Override
public void registerComponents(Context context, Glide glide) {
    glide.register(MyDataModel.class, Bitmap.class, new MyUrlLoader.Factory());
}

doesn't work for me. So to make custom loaded works I just use Glide Builder

private final GenericRequestBuilder<MyDataModel, Bitmap, Bitmap, Bitmap> mGlideBuilder;

    mGlideBuilder = Glide.with(mContext)
            .using(new MyUrlLoader(mContext), Bitmap.class)
            .from(MyDataModel.class)
            .as(Bitmap.class)
            .decoder(new MyBitmapDecoder())
            .diskCacheStrategy(DiskCacheStrategy.NONE);

    mGlideBuilder.load(entry).into(holder.icon);

and MyBitmapDecoder and MyUrlLoader declared as

    public class MyBitmapDecoder implements ResourceDecoder<Bitmap, Bitmap> {

    public class MyUrlLoader implements ModelLoader<MyDataModel, Bitmap> {
AndreyICE
  • 3,574
  • 29
  • 27