Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Out of memory exception with image

$
0
0

Hi,

I have a forms page with a grid. The grid has about 3 rows en 3 colums in each grid item is a imageview that does imagecaching. When I fill the grid with data in code I get a out of memoryexception for Android. I traced the problem to the images that are loading from a url. Please help.

StackTrace:
An unhandled exception occured.
12-23 09:16:55.671 E/mono (12017):
12-23 09:16:55.671 E/mono (12017): Unhandled Exception:
12-23 09:16:55.671 E/mono (12017): Java.Lang.OutOfMemoryError: Exception of type 'Java.Lang.OutOfMemoryError' was thrown.
12-23 09:16:55.671 E/mono (12017): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in :0
12-23 09:16:55.671 E/mono (12017): at Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1161
12-23 09:16:55.671 E/mono (12017): at Android.Graphics.BitmapFactory.DecodeStream (System.IO.Stream is) [0x00034] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Graphics.BitmapFactory.cs:614
12-23 09:16:55.671 E/mono (12017): at Android.Graphics.BitmapFactory+c__AnonStorey9.<>m__0 () [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Graphics.BitmapFactory.cs:621
12-23 09:16:55.671 E/mono (12017): at System.Threading.Tasks.TaskActionInvoker+FuncInvoke

1[Android.Graphics.Bitmap].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
    12-23 09:16:55.671 E/mono    (12017):   at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.671 E/mono    (12017):   at System.Threading.Tasks.Task.ThreadStart () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.671 E/mono    (12017): --- End of stack trace from previous location where exception was thrown ---
    12-23 09:16:55.671 E/mono    (12017):   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1+ConfiguredTaskAwaiter[Android.Graphics.Bitmap].GetResult () [0x00000] in :0
12-23 09:16:55.671 E/mono (12017): at Xamarin.Forms.Platform.Android.ImageLoaderSourceHandler+d__0.MoveNext () [0x00000] in :0
12-23 09:16:55.681 E/mono-rt (12017): at dalvik.system.NativeStart.run(N12-23 09:16:55.671 E/mono (12017): --- End of stack trace from previous location where exception was thrown ---
12-23 09:16:55.671 E/mono (12017): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in :0
12-23 09:16:55.671 E/mono (12017): at System.Runtime.CompilerServices.TaskAwaiter
1[Android.Graphics.Bitmap].GetResult () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.671 E/mono    (12017):   at Xamarin.Forms.Platform.Android.ImageRenderer+<UpdateBitmap>d__0.MoveNext () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.671 E/mono    (12017):   --- End of managed exception stack trace ---
    12-23 09:16:55.671 E/mono    (12017): java.lang.OutOfMemoryError: (Heap Size=49187KB, Allocated=45752KB)
    12-23 09:16:55.671 E/mono    (12017):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    12-23 09:16:55.671 E/mono    (12017):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:719)
    12-23 09:16:55.671 E/mono    (12017):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:791)
    12-23 09:16:55.671 E/mono    (12017):   at dalvik.system.NativeStart.run(Native Method)
    12-23 09:16:55.671 E/mono    (12017): 
    12-23 09:16:55.681 E/mono-rt (12017): [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.OutOfMemoryError: Exception of type 'Java.Lang.OutOfMemoryError' was thrown.
    12-23 09:16:55.681 E/mono-rt (12017):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.681 E/mono-rt (12017):   at Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1161 
    12-23 09:16:55.681 E/mono-rt (12017):   at Android.Graphics.BitmapFactory.DecodeStream (System.IO.Stream is) [0x00034] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Graphics.BitmapFactory.cs:614 
    12-23 09:16:55.681 E/mono-rt (12017):   at Android.Graphics.BitmapFactory+<DecodeStreamAsync>c__AnonStorey9.<>m__0 () [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Graphics.BitmapFactory.cs:621 
    12-23 09:16:55.681 E/mono-rt (12017):   at System.Threading.Tasks.TaskActionInvoker+FuncInvoke
1[Android.Graphics.Bitmap].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in :0
12-23 09:16:55.681 E/mono-rt (12017): at System.Threading.Tasks.Task.InnerInvoke () [0x00000] in :0
12-23 09:16:55.681 E/mono-rt (12017): at System.Threading.Tasks.Task.ThreadStart () [0x00000] in :0
12-23 09:16:55.681 E/mono-rt (12017): --- End of stack trace from previous location where exception was thrown ---
12-23 09:16:55.681 E/mono-rt (12017): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in :0
12-23 09:16:55.681 E/mono-rt (12017): at System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1+ConfiguredTaskAwaiter[Android.Graphics.Bitmap].GetResult () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.681 E/mono-rt (12017):   at Xamarin.Forms.Platform.Android.ImageLoaderSourceHandler+<LoadImageAsync>d__0.MoveNext () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.681 E/mono-rt (12017): --- End of stack trace from previous location where exception was thrown ---
    12-23 09:16:55.681 E/mono-rt (12017):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0 
    12-23 09:16:55.681 E/mono-rt (12017):   at System.Runtime.CompilerServices.TaskAwaiter
1[Android.Graphics.Bitmap].GetResult () [0x00000] in :0
12-23 09:16:55.681 E/mono-rt (12017): at Xamarin.Forms.Platform.Android.ImageRenderer+d__0.MoveNext () [0x00000] in :0
12-23 09:16:55.681 E/mono-rt (12017): --- End of managed exception stack trace ---
12-23 09:16:55.681 E/mono-rt (12017): java.lang.OutOfMemoryError: (Heap Size=49187KB, Allocated=45752KB)
12-23 09:16:55.681 E/mono-rt (12017): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-23 09:16:55.681 E/mono-rt (12017): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:719)
12-23 09:16:55.681 E/mono-rt (12017): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:791)
12-23 09:16:55.681 E/mono-rt (12017): at dalvik.system.NativeStart.run(Native Method)
12-23 09:16:55.681 E/mono-rt (12017):
The program 'Mono' has exited with code 0 (0x0).

Grid item content:
public class ContactPersonGridItem : StackLayout
{
public Image PersonImage;
public Label NameLabel;
public Person ContactPerson;

        public ContactPersonGridItem(Person p, int imageSize)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                ContactPerson = p;
                Orientation = StackOrientation.Vertical;
                Padding = new Thickness(0, 0, 0, 15);

                PersonImage = new Image()
                {

                    Source = new UriImageSource()
                    {
                        CachingEnabled = true,
                        CacheValidity = new TimeSpan(14, 0, 0, 0),
                        Uri = new Uri(ContactPerson.ImageUrl)
                    },
                    BackgroundColor = Color.Silver,
                    Aspect = Aspect.Fill,
                    WidthRequest = imageSize,
                    HeightRequest = imageSize
                };

                NameLabel = new Label()
                {
                    HorizontalOptions = LayoutOptions.Center,
                    Text = ContactPerson.LastAndFirstName,
                    TextColor = Color.FromHex(AppData.BasicColor),
                    Font = Device.OnPlatform(Font.OfSize(AppData.iOSFont, NamedSize.Large), Font.OfSize(AppData.AndroidFont, NamedSize.Large), Font.SystemFontOfSize(NamedSize.Large))
                };

                Children.Add(PersonImage);
                Children.Add(NameLabel);
            });
        }
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>