尝试使用适用于Android的Glide库。必须下载图片并将其显示在ImageView中。 如果这个类型url
http: //www.some.domen / storage / images / image.jpg
所有正常加载。 但是,如果您被拒绝访问登录名和密码,那么
http: // USER: PASSWORD@www.some.domen/storage/images
...
图片未加载。 出了什么问题,如何使用用户名和密码上传图片? 谢谢!
答案 0 :(得分:3)
基于https://stackoverflow.com/a/5137446/253468我认为这可能是一种解决方法/解决方案(未经测试):
LazyHeaders auth = new LazyHeaders.Builder() // can be cached in a field and reused
.addHeader("Authorization", new BasicAuthorization(username, password))
.build();
Glide
.with(context)
.load(new GlideUrl(url, auth)) // GlideUrl is created anyway so there's no extra objects allocated
.into(imageView);
public class BasicAuthorization implements LazyHeaderFactory {
private final String username;
private final String password;
public BasicAuthorization(String username, String password) {
this.username = username;
this.password = password;
}
@Override public String buildHeader() {
return "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64.NO_WRAP);
}
}