问题
I am developing an android app that needs to load many images from a folder that is located on my online server (in this case a GoDaddy hosting shared-plan).
All of the images are stored in an image folder inside the FileManager folder provided by GoDaddy.
Glide needs a URL to load an image, but in my case these images are not public and can't be reached from a HTTP URL (and should remain that way).
I would like to use glide to load these remotely stored images just like I am able to do so locally by providing a local path to the images on my local machine
For example this code works locally where path = (C:\Users\user\images\myImage.png) notice that it is not https:// url .
Glide.with(mContext)
.load(C:\Users\user\images\myImage.png)
.into(mImageView);
The path provided here is local and works on my local machine, I would like to replace localPath with remoteStorageFolderPath but I am unsure how it is done. Any help would be greatly appreciated! Thank you.
回答1:
so I think this has already been brought up as an issue in Glides Github, and was solved by TWiStErRob on 10 Nov 2016. The way to do it is to add an authorisation header as follows:
LazyHeaders auth = new LazyHeaders.Builder() // This can be cached in a field and reused later.
.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);
}
}
来源:https://stackoverflow.com/questions/49779759/using-glide-to-load-an-image-from-remote-storage