使用Glide从远程存储加载图像 [英] Using Glide to load an image from remote storage

查看:364
本文介绍了使用Glide从远程存储加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 android 应用,该应用需要从在线服务器(在本例中为GoDaddy托管共享计划)的文件夹中加载许多图像.

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).

所有图像都存储在GoDaddy提供的FileManager文件夹内的图像文件夹中.

All of the images are stored in an image folder inside the FileManager folder provided by GoDaddy.

Glide 需要一个URL来加载图像,但是在我的情况下,这些图像不是公开的,无法通过HTTP URL进行访问(并且应该保持这种状态).

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).

我想使用glide来加载这些远程存储的图像,就像我可以通过在本地计算机上提供图像的本地路径来本地加载图像一样

例如,此代码在本地工作,其中path =(C:\ Users \ user \ images \ myImage.png)注意到它不是不是 https://url.

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);

Glide.with(mContext) .load(C:\Users\user\images\myImage.png) .into(mImageView);

此处提供的路径是本地路径,可在我的本地计算机上运行,​​我想用remoteStorageFolderPath替换localPath ,但不确定如何完成.任何帮助将不胜感激! 谢谢.

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.

推荐答案

所以我认为这已经作为问题已在Glides Github中发布,并由TWiStErRob于2016年11月10日解决.解决方法是添加授权标头,如下所示:

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);
    }
}

这篇关于使用Glide从远程存储加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆