10

I am using Glide version = 4.7.1

I have no idea how to authenticate an image by glide. This code is what I did.

I created:

 private static final String AUTHORIZATION = "ss-id=doa3cx8OV3aGLThRrpnh;";
private static final String ABC = "application/json";

public static GlideUrl getUrlWithHeaders(String url){
    return new GlideUrl(url, new LazyHeaders.Builder()
            .addHeader("Cookie", AUTHORIZATION)
            .addHeader("Accept", ABC)
            .build());
}

}

String s = "http://192.168.1.144/api/download/" + mImageIds.get(position);
        GlideApp
                .with(mContext)
                .load(Headers.getUrlWithHeaders(s))
                .centerCrop()
                .transition(DrawableTransitionOptions.withCrossFade())
                .into((imageView));

Any helps,

Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

28

This is working code for Glide v3/v4:

String url = "http://192.168.1.144/api/download/" + mImageIds.get(position);

GlideUrl glideUrl = new GlideUrl(url, 
    new LazyHeaders.Builder()
            .addHeader("Cookie", AUTHORIZATION)
            .addHeader("Accept", ABC)
            .build());

Glide.with(this)
    .load(glideUrl)
    .into(imageView);
terencey
  • 3,282
  • 4
  • 32
  • 40
  • 1
    Great solution! Thanks – Francis Nov 22 '18 at 09:41
  • Awesome! suddenly works for me, but anyway why is it so hard to retrieve the image using Picasso with authorization header instead of Glide?. Cause I've been trying to get the images from a server through Picasso but still didn't work even the header has been initialized – XVallerie Jun 25 '20 at 14:05
8

Kotlin

val url:String="https://URL.com/" + data[position].image_path;

val glideUrl = GlideUrl(
            url,
            LazyHeaders.Builder()
                .addHeader("Authorization", "Bearer $token")
                .build()
)


 Glide.with(mContext)
  .load(glideUrl)
  .into(holder.binding.img);
Rohan Prasad
  • 151
  • 1
  • 3