6

I referred android developers site and created the volley NetworkImageView with singleton class. But its not maintaining the cache image after the force close of the app and again open my app in offline. how to make the network image view to get the image from cache in offline mode. Im using LruCache.

NOTE: I'm using NetworkImageView throughout the app. And I read somewhere that disk cache will be stored only when the image URL consists cache header. I want to know about that and also if no header in the URL like that then how to force the volley to store the disk cache of image ?

My code:

public class VolleySingletonPattern {

private static VolleySingletonPattern mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;

private VolleySingletonPattern(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(
            LruBitmapCache.getCacheSize(mCtx)));
}

public static synchronized VolleySingletonPattern getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new VolleySingletonPattern(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}}

My LruCache :

public class LruBitmapCache extends LruCache<String, Bitmap>
    implements ImageCache {

public LruBitmapCache(int maxSize) {
    super(maxSize);
}

public LruBitmapCache(Context ctx) {
    this(getCacheSize(ctx));
}

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

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

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

// Returns a cache size equal to approximately three screens worth of images.
public static int getCacheSize(Context ctx) {
    final DisplayMetrics displayMetrics = ctx.getResources().
            getDisplayMetrics();
    final int screenWidth = displayMetrics.widthPixels;
    final int screenHeight = displayMetrics.heightPixels;
    // 4 bytes per pixel
    final int screenBytes = screenWidth * screenHeight * 4;

    return screenBytes * 3;
}}
Stephenraj
  • 671
  • 6
  • 16
  • Possible duplicate of [How to make Volley NetworkImageView worke offline](http://stackoverflow.com/questions/26560531/how-to-make-volley-networkimageview-worke-offline) – Dhaval Parmar Feb 06 '16 at 08:18

1 Answers1

0

I have used this this , and is working for me. You need to add these permission.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Mobile Apps Expert
  • 928
  • 1
  • 7
  • 10
  • I want NetworkImageView to load images in offline, just like the facebook app did after it closed and re-opened in offline. I have read somewhere that facebook uses DiskCache to load bitmap images. If it true and DiskCache loads images in offline after closing the app and re-opened. Then how can i use that DiskCache in my NetworkImageView which has its own DiskCache. – Stephenraj Feb 06 '16 at 09:24
  • I have used the above link and it is working for offline also, if it is loaded once. – Mobile Apps Expert Feb 06 '16 at 11:01
  • I'm using NetworkImageView throughout the app. And I read somewhere that disk cache will be stored only when the image URL consists cache header. I want to know about that and also if no header in the URL like that then how to force the volley to store the disk cache of image. – Stephenraj Feb 09 '16 at 11:52