I am following up image cashing tutorial using Volley on Android developers, I am having problem with requesting an image request and caching it, I guess because of the singelton that I created (copied from the tutorial).
My Eclipse is giving error in the getInstance(this)
because this is context and I am requesting an image I guess.
ImageRequest request = new ImageRequest(
url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mNetworkImageView = (NetworkImageView) findViewById(R.id.ImageView);
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
// mImageLoader = MySingleton.getInstance(this).getImageLoader();
// mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,
// R.drawable.ic_launcher, R.drawable.ic_launcher));
}
},
0,
0,
null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
// mImageView.setImageResource(R.drawable.ic_launcher);
}
});
MySingleton.getInstance(this).addToRequestQueue(request);
This is singleton:
package com.example.p;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(
mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(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;
}
}
I am able to get the image and display it in such way , but I need to cache it , so I guess it to add it in a request right ? .. any help ?
mNetworkImageView = (NetworkImageView) findViewById(R.id.ImageView);
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
this
means, how use outer class inside anonymous interface implementation – Sheikdomthis
to update my knowledge about java, and if you know volley can you help me with my question ? – ComstockImageCache
atnew ImageLoader.ImageCache() { ... }
inMySingleton
class. – CrosspurposemImageLoader
) you get atmImageLoader = MySingleton.getInstance(this).getImageLoader();
has already been convined with ImageCache inMySingleton
. There is no need for additional operation for enabling caching. – CrosspurposesetImageUrl
onmNetworkImageView
, theimageLoader
doesImageRequest
internally. – CrosspurposeImageCache
is L1 Cache. That means it resides only during the app is launching. Volley also has internal L2 Cache (DiskBasedCache
). It's size is 5MB. – CrosspurposeDiskBasedCache
but I couldnt find anything helpful tutorial or examples about it especially there was some performance problems , your informations and help is really apreciated – ComstockDiskBasedCache
. And such discussions are beyond commenting. If you have further questions, please post new questions. – Crosspurpose