c# - BitmapFactory.DecodeByteArray causing Grow Heap (frag case) -



c# - BitmapFactory.DecodeByteArray causing Grow Heap (frag case) -

i developing android app in xamarin. having problems generating image bytestream. bitmapfactory (which seems popular solution), causing huge allocation problems - grow heap.

connecttodb connect = new connecttodb (); byte[] arr = connect.selectimgbyte(3,"thea"); bitmapfactory.options options=new bitmapfactory.options(); options.injustdecodebounds = true; bmp = bitmapfactory.decodebytearray (arr, 0, arr.length/*,options*/); _imageview.setimagebitmap (bmp);

above method bitmapfactory.decodebytearray beingness called. works fine, image displayed. slow , causes these "warnings".

thread started: <thread pool> #6 [dalvikvm-heap] grow heap (frag case) 22.596mb 1997584-byte allocation [choreographer] skipped 129 frames! application may doing much work on main thread. [dalvikvm-heap] grow heap (frag case) 20.755mb 1997584-byte allocation [dalvikvm-heap] grow heap (frag case) 22.735mb 1997584-byte allocation [dalvikvm-heap] grow heap (frag case) 24.710mb 1997584-byte allocation

for everytime method called, grow heap error appears. can see have loaded image imageview 4 times there. so, wondering if had same problem me? tried many hours solve problem, looking in here (and else where) not find solution. maintain in mind writing application in xamarin (c# language - using android library).

sorry lousy links, don't have plenty cred upload images here yet :)

first thing see utilize injustdecodebounds, used width , height of image so:

var options = new bitmapfactory.options { injustdecodebounds = true, }; using (var derp = bitmapfactory.decoderesource(resources, resource.id.myimage, options)) { } var imageheight = options.outheight; var imagewidth = options.outwidth;

this used aspect ratio of image, before scaling down, instead of loading huge image memory.

if take @ docs loading big bitmaps efficiently in xamarin documentation find utilize that.

then create sure load bitmap in using statement , assign imageview this:

using(var bmp = decodebitmap(some args)) imageview.setimagebitmap(bmp);

in cases don't need bitmap afterwards can phone call recycle() on it, tells java runtime rid of it:

using(var bmp = decodebitmap(some args)) { imageview.setimagebitmap(bmp); bmp.recycle(); }

so need similar, byte array contains pixels image.

so using pattern in docs this:

byte[] arr = connect.selectimgbyte(3,"thea"); using(var bmp = decodesampledbitmapfromarray(arr, scaledwidth, scaledheight)) { _imageview.setimagebitmap(bmp); bmp.recycle(); } public static int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image var height = (float)options.outheight; var width = (float)options.outwidth; var insamplesize = 1d; if (height > reqheight || width > reqwidth) { insamplesize = width > height ? height/reqheight : width/reqwidth; } homecoming (int) insamplesize; } public static bitmap decodesampledbitmapfromarray(byte[] pixels, int reqwidth, int reqheight) { var options = new bitmapfactory.options { injustdecodebounds = true, }; using (var dispose = bitmapfactory.decoderesource(arr, 0, arr.length, options)) { } // calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); // decode bitmap insamplesize set options.injustdecodebounds = false; homecoming bitmapfactory.decoderesource(arr, 0, arr.length, options); }

c# android bitmap xamarin bytearray

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -