Hi Jon, thank you for your time:)

I used the following class as a Imagehandler because I wanted to minimize
the use of memory in the app and have a cache of images ready.

In this scenario multiple activities share the same bitmap instance wrapped
in a weakreference. But sometimes a images goes missing - the reference is
still there but when the image is loaded in the imageview nothing is shown -
which is kind of weird. I am suspecting the weakreferences to be the issue.

Therefore I am not sure if this is the best approach to load images and keep
asking questions :)

public static class ImageHandler
    {
        public static Dictionary<string, WeakReference> ImageCache = new
Dictionary<string, WeakReference>();

        public static Bitmap DecodeFile(string _path)
        {
           
            WeakReference reference;
            var options = new BitmapFactory.Options {InPreferredConfig =
Bitmap.Config.Rgb565};
            Bitmap image=null;

            if (ImageCache.TryGetValue(_path, out reference))
            {

                try
                {
                  
                    if (reference.IsAlive)
                    {
                        if (reference.Target is Bitmap)
                        {
                            image = (Bitmap)reference.Target;
                        }


                        if ((reference.Target as Bitmap) == null)
                        {
                            // image has been garbage collected
                            // remove reference from cache
                            ImageCache.Remove(_path);
                        }
                        else
                        {

                            return image;
                        }
                    }
                    else
                    {
                        // image has been garbage collected
                        // remove reference from cache
                        ImageCache.Remove(_path);
                    }

                }
                catch (Exception)
                {
                    // image has been garbage collected
                    // remove reference from cache
                    ImageCache.Remove(_path);
                }

            }
            image = BitmapFactory.DecodeFile(_path, options);
            reference = new WeakReference(image);
            ImageCache.Add(_path, reference);
            return image;

        }
    }





--
View this message in context: 
http://mono-for-android.1047100.n5.nabble.com/Image-caching-tp5710451p5711466.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to