Android Volley Imageloader - Bitmaplrucache Parameter

Android Volley ImageLoader - BitmapLruCache parameter?

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;

return cacheSize;
}

public BitmapLruCache() {
this(getDefaultLruCacheSize());
}

public BitmapLruCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}

How to I get the cached Bitmap I downloaded using Volley ImageLoader?

Volley depends on your implementation of a cache for successful efficient caching.
The constructor for the ImageLoader takes in an ImageCache which is a simple interface in Volley to save and load bitmaps.

public ImageLoader(RequestQueue queue, ImageCache imageCache)

A quote from the Javadoc of ImageCache interface:

Simple cache adapter interface. If provided to the ImageLoader, it will be used as an L1 cache before dispatch to Volley. Implementations must not block. Implementation with an LruCache is recommended.

Darwind is right. If you request an image and it is present in the cache it will be loaded from the cache and not from the web. This should be the case for you since you're loading and presenting an image, which if clicked should be displayed from the cache in your new activity.

You say it's not working, perhaps your implementation isn't optimized for your use case. What kind of cache are you using? Do you have one centralized RequestQueue and ImageLoader as is recommended by the Volley team?

Take a look at this question, which isn't exactly the same as yours, yet could be helpful to you. It has a simple LRU cache implementation.

Hope that helps!

Edit:

The point of Volley is not to worry about implementation details. You want an image? it will load it for you the best and fastest way possible (from memory and if it's not there via network). That's exactly the way you should look at it. Retrieving the cache and then looking in it is not the right approach IMO.

Now, if you want to manipulate the bitmap, you have a few options, the best IMO being to implement your own Image Listener pass it to the get() method instead of the default one.

Something like this:

public class MyImageListener implements ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handle errors
}

@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {

//
// manipulations
//

// assuming mView is a reference to your ImageView
mView.setImageBitmap(bitmap);
} else {
// display placeholder or whatever you want
}
}
}

from the Javadoc:

The call flow is this:

  1. Upon being attached to a request, onResponse(response, true) will be invoked to reflect any cached data that was already available. If
    the data was available, response.getBitmap() will be non-null.

  2. After a network response returns, only one of the following cases will happen:

    • onResponse(response, false) will be called if the image was loaded.

      or

    • onErrorResponse will be called if there was an error loading the image.

Volley Image Caching

Volley did not gave caching option directly. you have to make your own with in the tool provide by Volley. See Network Image caching, Jake Wharton had written about caching mechanism using Volley. Jake Wharton's Volley Customization

Android Volley give me an outOfMemory exception

i did fix this to provide proper cache to the BitmapLruCache insted of LruCache

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;

return cacheSize;
}

public BitmapLruCache() {
this(getDefaultLruCacheSize());
}

public BitmapLruCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}

}

here is the link:
Android Volley ImageLoader - BitmapLruCache parameter?

Getting ImageLoader faster in Listview

Fastest option today is to use Volley.

Watch this Google IO talk: https://developers.google.com/events/io/sessions/325304728

And here is an example: http://cypressnorth.com/mobile-application-development/setting-android-google-volley-imageloader-networkimageview/

Just replace your ImageView by NetworkImageView in your XML like ths:

<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />

And in your source code (in your adapter), you set the URL like this:

yourNetworkImageViewReference.setImageUrl( "you_url_here" , MyVolley.getImageLoader() );

PS: You will also need the BitmapLruCache class you can find on this post: Android Volley ImageLoader - BitmapLruCache parameter?.

Volley : Image Caching

For image caching, volley expects you to provide an implementation memory cache for images. This cache is used during the up time of the app for quicker loading times using the memory.

Not related specifically to images, Volley has its own disk cache which it uses to cache EVERY response it gets, with the default strategy of caching according to cache headers of the HTTP response.

If the images you are loading in your app have cache headers, they will be cached according to them on the disk, otherwise the will not be.

If you're unhappy with this strategy and want to force disk caching, you'll have to edit / add a little code that changes the caching strategy. There are many ways to achieve this, one being providing your own implementation to parsing the HTTP headers. Take a look at HttpHeaderParser in the Volley source.



Related Topics



Leave a reply



Submit