2

Trying Glide library for Android. It is necessary to download pictures and display it in the ImageView. If this type url

http: //www.some.domen / storage / images / image.jpg

all normal loaded. But if you are denied access login and password so

http: // USER: PASSWORD@www.some.domen/storage/images ...

the picture is not loaded. What's wrong, how do I upload a picture using the username and password? Thank you!

Sam Judd
  • 7,317
  • 1
  • 38
  • 38
sulik
  • 23
  • 4

1 Answers1

4

Based on https://stackoverflow.com/a/5137446/253468 I think this could be a workaround/solution (untested):

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);
    }
}
Community
  • 1
  • 1
TWiStErRob
  • 44,762
  • 26
  • 170
  • 254