Glide - adding header to request

前端 未结 8 1301
北海茫月
北海茫月 2021-02-02 09:25

is there a method to add custom header to request when image is downloaded? I can use volley or okhttp in Glide.

I try add cookie to cookiemanager in okhttpclient, but i

相关标签:
8条回答
  • 2021-02-02 09:47

    Since 3.6.0 it's possible to set custom headers for each request:

    GlideUrl glideUrl = new GlideUrl("url", new LazyHeaders.Builder()
        .addHeader("key1", "value")
        .addHeader("key2", new LazyHeaderFactory() {
            @Override
            public String buildHeader() {
                String expensiveAuthHeader = computeExpensiveAuthHeader();
                return expensiveAuthHeader;
            }
        })
        .build());
    
    Glide....load(glideUrl)....;
    
    0 讨论(0)
  • 2021-02-02 09:48

    Interceptors sound like a great choice. You can pass in your own instance of an OkHttp client to an OkHttpUrlLoader.Factory and register the Factory with Glide.

    If you want more control, you can also simply fork the OkHttp ModelLoader and DataFetcher, register your forked ModelLoader, and get direct access to the OkHttp client for every request.

    0 讨论(0)
  • 2021-02-02 09:50

    Kotlin + Glide 4.10.0

    val token = "..."
    val url = https://url.to.your.image
    val glideUrl = GlideUrl(url) { mapOf(Pair("Authorization", "Bearer $token")) }
    
    Glide.with(context)
         .load(glideUrl)
         .into(imageView)
    
    0 讨论(0)
  • 2021-02-02 09:54

    Try this:

    ImageView imgThumb = itemView.findViewById(R.id.image_thumb);
    
    GlideUrl url = new GlideUrl("https://your-url.com", new LazyHeaders.Builder()
                    .addHeader("User-Agent", "your-user-agent")
                    .build());
    
    RequestOptions options = new RequestOptions()
        .diskCacheStrategy(DiskCacheStrategy.NONE);
    
    Glide.with(mContext).load(glideUrl)
                        .transition(withCrossFade())
                        .thumbnail(0.5f)
                        .apply(options)
                        .into(imgThumb);
    

    The Glide reference is:

    implementation 'com.github.bumptech.glide:glide:4.6.1'
    
    0 讨论(0)
  • 2021-02-02 09:58

    Please read this thread: https://github.com/bumptech/glide/issues/198

    It appears as if it will be implemented in the coming release (4.0).

    0 讨论(0)
  • 2021-02-02 10:06

    This is late, I think but I put it here for notes for anyone else facing the issue. Here are my code samples (this is for Glide v4):

    BaseGlideUrlLoader module:

    private static class HeaderedLoader extends BaseGlideUrlLoader<String> {
    
        public static final Headers HEADERS = new LazyHeaders.Builder()
                .addHeader("Referer", UserSingleton.getInstance().getBaseUrl())
                .build();
    
        public HeaderedLoader(ModelLoader<GlideUrl, InputStream> concreteLoader) {
            super(concreteLoader);
        }
    
        @Override public boolean handles(@NonNull String model) {
            return true;
        }
    
        @Override protected String getUrl(String model, int width, int height, Options options) {
            return model;
        }
    
        @Override protected Headers getHeaders(String model, int width, int height, Options options) {
            return HEADERS;
        }
    
        public static class Factory implements ModelLoaderFactory<String, InputStream> {
    
            @Override public @NonNull ModelLoader<String, InputStream> build(
                    @NonNull MultiModelLoaderFactory multiFactory) {
                return new HeaderedLoader(multiFactory.build(GlideUrl.class, InputStream.class));
            }
    
            @Override public void teardown() { /* nothing to free */ }
        }
    }
    

    Add the HeaderLoader class in the AppGlideModule.

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
    
        registry.replace(GlideUrl.class, InputStream.class,
                new OkHttpUrlLoader.Factory(CustomOkHttpsClient.getTrustedOkHttpClient()
                ));
    
        // override default loader with one that attaches headers
        registry.replace(String.class, InputStream.class, new HeaderedLoader.Factory());
    }
    

    My solution is based on link provided here https://github.com/TWiStErRob/glide-support/commit/b357427363c28a82495097ec862b82acdf3b4357

    The original issue is discussed here https://github.com/bumptech/glide/issues/471

    Thanks to @TWiStErRob

    0 讨论(0)
提交回复
热议问题