问题
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,
回答1:
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);
回答2:
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);
来源:https://stackoverflow.com/questions/51149546/how-to-add-authorization-in-header-by-glide-in-android-studio