1

I'm trying to use an LRUcache to cache some images that my app downloads from web. I've read the tutorial from android developers but i can't manage to add anything at my LRUcache. here is my code,

class myloader extends Activity{ String url;ImageView imview;Bitmap map;LruCache<String, Bitmap> myCache=new LruCache<String, Bitmap> (5*1024*1024)  
    class load extends AsyncTask<String, Void, Bitmap>{
    WeakReference <ImageView> ref=new WeakReference<ImageView>(imview);
    String url2;
    public load(ImageView imageView) {  
        ref = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(String... params) {                 
        try {
             u=new URL(params[0]);           
             map=BitmapFactory.decodeStream(u.openConnection().getInputStream(),null,listDimensions());
             myCache.put(url,map);
        } catch (MalformedURLException e) {e.printStackTrace();
        } catch (IOException e) {e.printStackTrace();}
        return map;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (ref != null) {
            ImageView imageView = ref.get();
            toLoad bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
           if (this == bitmapDownloaderTask) {
                imageView.setImageBitmap(result);
            }
        }
    }  public void download(String theurl,ImageView im) {   url=theurl;  // here i check for lrucache items
    imview=im;
     if (cancelPotentialDownload(url, imview)) {
         toLoad task = new toLoad(imview);
         DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
         Bitmap finalBit=myCache.get(url);

         if (finalBit!=null){
             imview.setImageBitmap(finalBit);
         }else{
         imview.setImageDrawable(downloadedDrawable);
         task.execute(url);
         }
        t=myCache.putCount();
     }
}

then from getview() of my adapter class i call myloader, passing an imageview and an image url

tasgr86
  • 299
  • 2
  • 7
  • 17
  • 2
    Add some more info. Is the `cache` open? What exception do you get? – Jordy Langen Dec 18 '12 at 12:49
  • What you mean by open?I don't get any exceptions.When i later try to get those bitmaps and display them, i can't.I also used the size() method and got zero entries.So i conclude that nothing was added to my cache. – tasgr86 Dec 18 '12 at 13:01
  • You cannot use constructors in classes that `extend Activity`. Activities are created by the Android framework and the framework won't call your constructor. – David Wasser Dec 18 '12 at 13:11
  • did't know that!!I deleted the constructor.Now i create on my adapter class an object of myloader class, an then i call the download(String s,ImageView i, LRUcache l) method.But the problem remains – tasgr86 Dec 18 '12 at 13:30
  • also when i call putCount() method inside download(), it returns 0 so nothing was put in lrucache at doinbackground – tasgr86 Dec 18 '12 at 14:16

1 Answers1

0

I had the same problem. Make sure the cache size you allocated is big enough to accept one element. LruCache will not complain if you try to insert an element that is too large, it will fail silently.

ian
  • 695
  • 2
  • 9
  • 19