访问的通用图像装载机受保护的图像图像、装载机

2023-09-07 15:32:02 作者:爷蹲街,戏小妞

我正在使用的应用程序,需要获取图像的授权源的通用图像加载器。

I am using the universal image loader in an app that needs to fetch images an authorized source.

到目前为止,我已经扩展了URLConnectionImageDownloader类和我自己的类,并且覆盖的方法getStreamFromNetwork用我自己的实现,设置在URLConnection对象本身的授权头:

So far, I have extended the URLConnectionImageDownloader class with my own class, and overridden the method getStreamFromNetwork with my own implemetation that sets the authorization header in the URLConnection object as such:

public class authURLConnectionImageDownloader extends URLConnectionImageDownloader {

@Override
public InputStream getStreamFromNetwork(URI imageUri) throws IOException {

    String auth = Base64.encodeToString(("username" + ":"+"psswd").getBytes(), Base64.NO_WRAP);

    URLConnection conn = imageUri.toURL().openConnection();
    conn.setRequestProperty("Authorization", "Basic " + auth);

    conn.setConnectTimeout(DEFAULT_HTTP_CONNECT_TIMEOUT);
    conn.setReadTimeout(DEFAULT_HTTP_READ_TIMEOUT);

    return new FlushedInputStream(new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE));     
}

和设立我ImageLoader的......

and for setting up my ImageLoader...

imageLoader = ImageLoader.getInstance();

ImageLoaderConfiguration config =  new ImageLoaderConfiguration.Builder(MainActivity.this)
        .imageDownloader(new authURLConnectionImageDownloader())
        .build();

imageLoader.init(config);

到目前为止,我无法得到它的工作。图像不下载。但更重要的是我已经把getStreamFromNetwork(断点),它是永远不会打?我究竟做错了什么?

So far I have unable to get it to work. The image is not downloaded. But more importantly I have put a breakpoint in getStreamFromNetwork() and it is never hit? What am I doing wrong?

推荐答案

我设法得到它到底工作...

I managed to get it working in the end...

我使用的库的.jar与源自带的例子,并调试它。我看到了,这是从来没有访问我URLConnectionImageDownloader派生类中,总是使用父。

I used the library .jar with source that comes with the example, and debugged it. I saw that it was never accessing my URLConnectionImageDownloader derived class and always using the parent.

所以,看着我的code,看见我这是使用默认URLConnectionImageDownloader类previous活动中设置了另一个ImageLoader的。

So looked at my code, and saw I was setting up another imageloader within a previous activity that was using the default URLConnectionImageDownloader class.

现在我已经创建了一个应用程序类,并设置我的ImageLoader的一次(如示例应用程序),并设置配置为使用我的新authURLConnectionImageDownloader类。现在,我所有的activies使用ImageLoader的和它的作品。

Now I have created an application class and setup my ImageLoader once (as in the example app), and set the config to use my new authURLConnectionImageDownloader class. Now all my activies use this ImageLoader and it works.