loopj / android-smart-image-view

Android ImageView replacement which allows image loading from URLs or contact address book, with caching
http://loopj.com/android-smart-image-view/
1.3k stars 510 forks source link

Loading image from URL with authentication #17

Open lawrenz opened 11 years ago

lawrenz commented 11 years ago

How can i use this if i try to load from a URL that has basic authentication?

wolfhechel commented 10 years ago

Not sure if this is any interest anymore, but the way I solved this was to use a code similar to this;

String userInfo = Uri.parse(imagePathString).getUserInfo();

if (userInfo != null) {
    String[] components = userInfo.split(":", 1);

    final String username = components[0],
                 password = (components.length > 1) ? components[1] : "";

    Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password.toCharArray());
                }
    });
}

Because all my images are at the same base path I'll call this in my Application.onCreate function and pass my base path as imagePathString. In another scenario where you have images on different basic authentication protected spaces you'd have to call this code before each request going through to Smart Image View.