How to add authorization in header by Glide in Android Studio?

我怕爱的太早我们不能终老 提交于 2021-02-07 14:21:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!