android - Out Of Memory on Assets.Reload after Purchasing Item -
i developed game use 2 textures (one 2048x2048 , other 1024x2048) beside use 7 background pictures level images (each 640x960).
i got out of memory (oom) when payment successful pop out (inapp billing), traced log , got error come out when run onsurfacecreated code assets.reload executed textures , background pictures reloaded.
don't know since if don't reload not proper image.
fyi : assets.reload called when pause/resume executed (when pressed home button while playing) never become problem.
public class texture { glgraphics glgraphics; fileio fileio; string filename; int textureid; int minfilter; int magfilter; int width; int height; public texture(glgame glgame, string filename) { this.glgraphics = glgame.getglgraphics(); this.fileio = glgame.getfileio(); this.filename = filename; load(); } private void load() { gl10 gl = glgraphics.getgl(); int[] textureids = new int[1]; gl.glgentextures(1, textureids, 0); textureid = textureids[0]; inputstream in = null; try { in = fileio.readasset(filename); bitmap bitmap = bitmapfactory.decodestream(in); width = bitmap.getwidth(); height = bitmap.getheight(); gl.glbindtexture(gl10.gl_texture_2d, textureid); glutils.teximage2d(gl10.gl_texture_2d, 0, bitmap, 0); setfilters(gl10.gl_nearest, gl10.gl_nearest); gl.glbindtexture(gl10.gl_texture_2d, 0); } catch(ioexception e) { throw new runtimeexception("couldn't load texture '" + filename + "'", e); } { if(in != null) try { in.close(); } catch (ioexception e) { } } } public void reload() { load(); bind(); setfilters(minfilter, magfilter); glgraphics.getgl().glbindtexture(gl10.gl_texture_2d, 0); } public void setfilters(int minfilter, int magfilter) { this.minfilter = minfilter; this.magfilter = magfilter; gl10 gl = glgraphics.getgl(); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_min_filter, minfilter); gl.gltexparameterf(gl10.gl_texture_2d, gl10.gl_texture_mag_filter, magfilter); } public void bind() { gl10 gl = glgraphics.getgl(); gl.glbindtexture(gl10.gl_texture_2d, textureid); } public void dispose() { gl10 gl = glgraphics.getgl(); gl.glbindtexture(gl10.gl_texture_2d, textureid); int[] textureids = { textureid }; gl.gldeletetextures(1, textureids, 0); } }
as can see @ code (this mario zachner beginning android games framework) , assets.reload come texture.reload.
oom happened in real devices (tested on samsung galaxy young , samsung galaxy note ii)
additional question : purchase process forced app enter pause mode ?
updated / response answer : per suggestion, modified code :
in = fileio.readasset(filename); bitmapfactory.options bfoptions=new bitmapfactory.options(); bfoptions.injustdecodebounds = true; bitmap bitmap = bitmapfactory.decodestream(in,null,bfoptions); int imageheight = bfoptions.outheight; int imagewidth = bfoptions.outwidth; bfoptions.injustdecodebounds = false; bfoptions.insamplesize = calculateinsamplesize(bfoptions,imagewidth,imageheight); bfoptions.indither=false; //disable dithering mode bfoptions.inpurgeable=true; //tell gc whether needs free memory, bitmap can cleared bfoptions.ininputshareable=true; //which kind of reference used recover bitmap data after being clear, when used in future bfoptions.intempstorage=new byte[32 * 1024]; bitmap = bitmapfactory.decodestream(in,null,bfoptions); width = bitmap.getwidth(); height = bitmap.getheight(); gl.glbindtexture(gl10.gl_texture_2d, textureid); glutils.teximage2d(gl10.gl_texture_2d, 0, bitmap, 0); setfilters(gl10.gl_nearest, gl10.gl_nearest); gl.glbindtexture(gl10.gl_texture_2d, 0);
however when run got other error : still oom but, said failed allocation scaled bitmap
you should refcator
bitmap bitmap = bitmapfactory.decodestream(in);
you can use
public static bitmap decodesampledbitmapfromfile(string photopath) { bitmapfactory.options options = new bitmapfactory.options(); options.inpreferredconfig = bitmap.config.argb_8888; //here add more optiouns optimize // options.insamplesize = ... bitmap bitmap = bitmapfactory.decodefile(photopath, options); return bitmap; }
have here android image resize
Comments
Post a Comment