Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 / 6;
- 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);
- }
- }
- public class VolleyHelper {
- private static final int MAX_IMAGE_CACHE_ENTIRES = 100;
- private static RequestQueue mRequestQueue;
- private static ImageLoader mImageLoader;
- private VolleyHelper() {
- }
- public static void init(Context context) {
- mRequestQueue = Volley.newRequestQueue(context);
- mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(MAX_IMAGE_CACHE_ENTIRES));
- }
- public static RequestQueue getRequestQueue() {
- if (mRequestQueue != null) {
- return mRequestQueue;
- } else {
- throw new IllegalStateException("RequestQueue not initialized");
- }
- }
- public static ImageLoader getImageLoader() {
- if (mImageLoader != null) {
- return mImageLoader;
- } else {
- throw new IllegalStateException("ImageLoader not initialized");
- }
- }
- }
- ImageLoader mImageLoader = VolleyHelper.getImageLoader();
- if (mImageLoader.isCached(url, maxWidth, maxheight)) {
- //always false
- }
- /**
- * Checks if the item is available in the cache.
- * @param requestUrl The url of the remote image
- * @param maxWidth The maximum width of the returned image.
- * @param maxHeight The maximum height of the returned image.
- * @return True if the item exists in cache, false otherwise.
- */
- public boolean isCached(String requestUrl, int maxWidth, int maxHeight) {
- throwIfNotOnMainThread();
- String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
- return mCache.getBitmap(cacheKey) != null;
- }
- if (mImageLoader.isCached(imageUrl, 0, 0)) {
- // code here...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement