Forum Xamarin.Android

Gallery/ImageView explodes memory

Hi,

I'm developing a Xamarin Forms app. Since there is no gallery view on Xamarin Forms, I'm currently writing my client side image gallery for each platform. On Android, I have encountered the following problem:

Before I start the gallery activity, I have a memory footprint of approx. 90MB. After loading a gallery with two images (size 800kb on disk), the footprint explodes to almost 200MB. The swiping is stuttering, and shortly afterwards the app crashes with an OutOfMemory exception. Is this a common problem? How do you guys solve it? I have read about scaling down images using BitmapFactory, however the images are small and don't need to be scaled down. Or am I missing something?

Kind regards,
Max

Tagged:

Best Answer

Answers

  • MaximilianMantzMaximilianMantz DEMember

    I have implemented the suggestions from the Android resources (Load Large Bitmaps Effectively). It had no effect on the performance. I also see that the Android Gallery is obsolete. What alternatives to the Gallery do I have?

  • MaximilianMantzMaximilianMantz DEMember
    edited July 2015

    Bumping this again to provide further info about my problem:

    The reason there is a ViewModel referenced is because a Xamarin Forms app is running in the background and this is a native control to allow me to use a gallery (which Xamarin Forms doesn't provide).

    My activity is setup like this:

        protected async override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            translator = Resolver.Resolve<ITranslator>();
            viewModel = Resolver.Resolve<InspectionViewModel>();
    
            Title = translator.Translate("GalleryTitle");
            // Create your application here
            SetContentView(Resource.Layout.Main);
    
            Gallery gallery = (Gallery)FindViewById<Gallery>(Resource.Id.gallery);
            paths = viewModel.PicturePaths.ToList();
            gallery.Adapter = new ImageAdapter(this, paths);
            gallery.ItemLongClick += async delegate(object sender, AdapterView.ItemLongClickEventArgs e)
            {
                    DoSomething();
            };
        }
    

    With an ImageAdapter setup like this;

    public class ImageAdapter : BaseAdapter 
    {
        Context context;
        public List<string> paths;
    
        public ImageAdapter(Context c, IEnumerable<string> paths)
        {
            context = c;
            this.paths = paths.ToList();
        }
    
    
    
        public override int Count
        {
            get
            {
                return paths.Count();
            }
        }
    
        public override Java.Lang.Object GetItem(int position)
        {
            return 0;
        }
    
        public override long GetItemId(int position)
        {
            return 0;
        }
    
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            ImageView i = new ImageView(context);
    
            i.SetImageURI(Android.Net.Uri.Parse(paths[position]));
    
            i.SetScaleType(ImageView.ScaleType.FitXy);
    
            return i;
        }
    }
    

    I've tried the setup using SetImageBitmap() and using the recipe to reduce the image file, but the effect is the same, so I've switched back to parsing the URI. As I said previously, the pictures are very small, so it having no effect didn't surprise me.

    Running like this, the memory usage of the app increases exponentially, the gallery is slow to react and stuttering when swiping until eventually an OutOfMemory exception is thrown. What could be causing this?

  • MaximilianMantzMaximilianMantz DEMember

    Great stuff, thank you! Using Universal Image Loader, the stuttering is gone and I have fine grained control over which items are in the cache and which ones aren't, as well as being able to set limits for the memory cache to prevent it from overloading.

    I also didn't know that this was a general Android problem. So thanks again, you've helped me out a lot.

Sign In or Register to comment.