0

I have a Custom AdapterView (sort of) in which I lazy load images. To do it, I use the awesome aquery library.

Short story: I would like to cache (memcache and filecache) the downsampled version of the file. It makes it quicker to add to my adapter - when the image is small I have no lags when scrolling my AdapterView. When the image is big, even if I use downsampling it lags a bit. I found out, that aquery stores the full version of image and downsamples it every time I call aq.image(...). How to cache the resized version, not the original one?


Long story: My AdapterView relies heavily on images. These images are rather big, and when adapter items gets instantiated it takes some time to downsample it and then add to the list. So I thought it would be nice, to instantiate items with a lo-res photo when scrolling, and only load the hi-res version when the scrolling stops. It works like a charm, when I use two separate image urls (one for thumnbail and another for the original image). But the API I work with is quite limited, so I won't have the thumbnail images' urls. I have to async download the big version, downsample it, save both big and small version and then use whichever I need. And here the "short story" begins.

Michał Klimczak
  • 11,170
  • 7
  • 55
  • 92
  • Did you finally manage to get it work with aquery or you switched to some alternatives? – JanBo Mar 09 '15 at 18:07
  • I switched originally from picasso to aquery since picasso would fail if i tried to download and show extra large images( larger the 2048px ...didnt do downsampling properly)...but i might give it a go again if you didnt have those problems... – JanBo Mar 09 '15 at 18:11

2 Answers2

2

I just released an open source library called droidQuery which is a full port of jQuery to Android. It is much simpler to use than AQuery, and provides a lot more configurations. To download an image and set the output size, and cache the small image (on a per-session/every ten minute basis), you can use the following code:

final ImageView image = (ImageView) findViewById(R.id.myImage);
$.ajax(new AjaxOptions(url).type("GET")
                           .dataType("image")
                           .imageHeight(200)
                           .imageWidth(200)
                           .context(this)
                           .cache(true)
                           .cacheTimeout(600000)
                           .success(new Function() {
                               @Override
                               public void invoke($ droidQuery, Object... params) {
                                   $.with(image).val((Bitmap) params[0]);
                               }
                           })
                           .error(new Function() {
                               @Override
                               public void invoke($ droidQuery, Object... params) {
                                   droidQuery.toast("could not set image", Toast.LENGTH_SHORT);
                               }
                           }));
Phil
  • 34,061
  • 21
  • 117
  • 154
1

I also used AQuery library in the past, but after encountering some problems with limited configuration and weird progresbar visibility issue, I moved to Android-Universal-Image Loader

https://github.com/nostra13/Android-Universal-Image-Loader it gives you your needed feature as well as plenty other useful configuration options.

Just read this site from top to bottom - and you should be able to run it in a minute.

In your case most interesting lines are

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
    .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
    .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75)
    .discCache(new UnlimitedDiscCache(cacheDir)) // default
    .discCacheSize(50 * 1024 * 1024)
    .discCacheFileCount(100)

You can also change cached file names.

MP23
  • 1,693
  • 20
  • 25
  • Thanks, I'll take a look. I love the simplicity of AQuery and I hope it's possible to do it with that lib, but I read the source for two hours and can't find anything... Maybe AUIL will suit me better – Michał Klimczak Mar 09 '13 at 21:41
  • Unfortunately this: `memoryCacheExtraOptions(50, 50)` can only be set in the `init` phase. I can't set the thumbnails size in per-file basis. Plus, it doesn't seem to use my settings. I set it to (50, 50), but the bitmap is still hi-res. – Michał Klimczak Mar 09 '13 at 23:28