Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let Volley's NetworkImageView show local image files

I am using NetworkImageView to show some covers downloaded from a remote URL and I successfully manage to cache and show them, but I want to let users set their own cover images if they want. I tried to use setImageUrl method with Uri.fromFile(mCoverFile).toString() as arguments, but it doesn't work. Since it is a mix of remote and local images I can't switch to regular ImageViews, so I was wondering if there's any way to enable loading of local images.

I am of course aware of the ImageView's setImageBitmap method, but NetworkImageView automatically resizes the created Bitmap and also prevents View recycling in GridViews and ListViews.

UPDATE: njzk2's answer did the trick. To autoresize the Bitmap according to your View size, then just copy the ImageRequest.doParse method from Volley's source.

like image 314
Vektor88 Avatar asked Mar 17 '14 20:03

Vektor88


1 Answers

NetworkImageView uses ImageLoader, which in turn uses an ImageCache.

You can provide a custom ImageCache with your images, provided you use the same mechanism for keys:

 return new StringBuilder(url.length() + 12).append("#W").append(maxWidth)
            .append("#H").append(maxHeight).append(url).toString();

url is not tested before the actual request would be done, so no issue here.

Typically, your 'cache' could look like :

public class MyCache implements ImageLoader.ImageCache {

    @Override
    public Bitmap getBitmap(String key) {
        if (key.contains("file://")) {
            return BitmapFactory.decodeFile(key.substring(key.indexOf("file://") + 7));
        } else {
            // Here you can add an actual cache
            return null;
        }
    }
    @Override
    public void putBitmap(String key, Bitmap bitmap) {
        // Here you can add an actual cache
    }
}

You use it like :

imageView.setImageUrl(Uri.fromFile(mCoverFile).toString(), new MyCache());

(This has not been actually tested and there may be some adjustments to do)

like image 147
njzk2 Avatar answered Sep 18 '22 02:09

njzk2