Show image from the folder of a protected server with .htpasswd

1

I have a app in android showing images with this plugin . The problem is that I have added a .htpasswd to protect the images and now it does not show them to me, I have added to the url the parameters user:password@ but now I get an error in the console:

java.io.FileNotFoundException:

The code I use is the following:

imageLoader.displayImage(tempValues.getImagen(), holder.imagen, options, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

    }
});
ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
DisplayImageOptions options = new DisplayImageOptions.Builder()
        .delayBeforeLoading(10)
        .cacheOnDisk(true)
        .cacheInMemory(true)
        .build();

Is there any way to add the user and password in the options of the imageLoader?

    
asked by 23.06.2017 в 09:16
source

1 answer

1

The serial image loader has nothing to set user and password to load URLs.

You'll have to create your own class.

Try this stackoverflow in English :

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

Now you create your own ImageDownloader:

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

and you put the configuration:

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

    ImageLoader.getInstance().init(config);
    
answered by 23.06.2017 / 11:15
source