2012-11-27 19 views
5

私は、許可されたソースを画像を取得する必要のあるアプリでユニバーサル画像ローダーを使用しています。ユニバーサル画像ローダーで保護された画像にアクセスする

これまでのところ、私は自分のクラスにURLConnectionImageDownloaderクラスを拡張し、そのようにURLConnectionオブジェクトに許可ヘッダーを設定し、自分自身の実装でメソッドgetStreamFromNetworkをオーバーライドしています

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

imageLoader = ImageLoader.getInstance(); 

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

imageLoader.init(config); 

これまでのところ、私はそれを動作させることができませんでした。画像はダウンロードされません。しかしもっと重要なことに、私はgetStreamFromNetwork()にブレークポイントを設定しました。私は間違って何をしていますか?

+0

感謝を解決するために! :) – Sadegh

答えて

4

私は例が付属していますソースのライブラリの.jarを使用し、それをデバッグ...それは終わりで働い取得する

を管理していました。 URLConnectionImageDownloaderの派生クラスにアクセスすることは決してなく、常に親を使用していることがわかりました。

私のコードを見て、デフォルトのURLConnectionImageDownloaderクラスを使用していた以前のアクティビティで別のimageloaderを設定していました。

私はアプリケーションクラスを作成してImageLoaderをセットアップしました(このサンプルアプリケーションのように)、新しいauthURLConnectionImageDownloaderクラスを使用するようにconfigを設定しました。今では私のすべての活動家がこのImageLoaderを使用しています。

byte[] toEncrypt = (username + ":" + password).getBytes(); 
     String encryptedCredentials = Base64.encodeToString(toEncrypt, Base64.DEFAULT); 
     Map<String, String> headers = new HashMap(); 
     headers.put("Authorization","Basic "+encryptedCredentials); 

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() 
      ... 
      .extraForDownloader(headers) 
      .build(); 

独自ImageDownloaderを作成します:

12

は、私はそれをこのように実装

public class AuthDownloader extends BaseImageDownloader { 

    public AuthDownloader(Context context){ 
     super(context); 
    } 

    @Override 
    protected HttpURLConnection createConnection(String url, Object extra) throws IOException { 
     HttpURLConnection conn = super.createConnection(url, extra); 
     Map<String, String> headers = (Map<String, String>) extra; 
     if (headers != null) { 
      for (Map.Entry<String, String> header : headers.entrySet()) { 
       conn.setRequestProperty(header.getKey(), header.getValue()); 
      } 
     } 
     return conn; 
    } 
} 

をし、コンフィギュレーションに設定します。

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) 
      .defaultDisplayImageOptions(defaultOptions) 
      .discCacheExtraOptions(600, 600, CompressFormat.PNG, 75, null) 
      .imageDownloader(new AuthDownloader(getApplicationContext())) 
      .build(); 

    ImageLoader.getInstance().init(config); 
+0

createConnectionの追加パラメータが常にnullを返しています。 – Ricardo

+0

は完璧に動作します。あなたは私の日を救う。どうもありがとう。 –

関連する問題