i'm goning resize bitmap , insert imageview.
however encountered recycle error...
error message :
05-08 13:32:48.948: e/androidruntime(4792): process: com.test.myapp, pid: 4792 05-08 13:32:48.948: e/androidruntime(4792): java.lang.runtimeexception: canvas: trying use recycled bitmap android.graphics.bitmap@2f1aa6ad 05-08 13:32:48.948: e/androidruntime(4792): @ android.graphics.canvas.throwifcannotdraw(canvas.java:1225) 05-08 13:32:48.948: e/androidruntime(4792): @ android.view.gles20canvas.drawbitmap(gles20canvas.java:600) 05-08 13:32:48.948: e/androidruntime(4792): @ android.graphics.drawable.bitmapdrawable.draw(bitmapdrawable.java:544) 05-08 13:32:48.948: e/androidruntime(4792): @ android.widget.imageview.ondraw(imageview.java:1187) 05-08 13:32:48.948: e/androidruntime(4792): @ android.view.view.draw(view.java:16209)
could give advice how resolve situation..
my code :
bitmap photo = bitmapfactory.decodefile(full_path); savebitmaptofilecache(photo, environment.getexternalstoragedirectory().getpath() + "/test/"+filename); imgview.setimagebitmap(photo); private bitmap getbitmapsize(bitmap photo){ bytearrayoutputstream os = new bytearrayoutputstream(); bitmap bmp = photo; bitmap resizebmp = null; if(bmp.getwidth() > 2000 || bmp.getheight() > 2000) resizebmp = resizingbitmap(mcontext, bmp, bmp.getwidth()/10 , bmp.getheight()/10, false); else if(bmp.getwidth() > 1000 || bmp.getheight() > 1000) resizebmp = resizingbitmap(mcontext, bmp, bmp.getwidth()/5 , bmp.getheight()/5, false); else resizebmp = resizingbitmap(mcontext, bmp, bmp.getwidth() , bmp.getheight(), false); resizebmp.compress(compressformat.png, 90, os); bmp.recycle(); return resizebmp; } private void savebitmaptofilecache(bitmap bitmap, string strfilepath) { file filecacheitem = new file(strfilepath); outputstream out = null; try{ filecacheitem.createnewfile(); out = new fileoutputstream(filecacheitem); bitmap.compress(compressformat.jpeg, 100, out); } catch (exception e) { e.printstacktrace(); } finally{ try{ out.close(); } catch (ioexception e){ e.printstacktrace(); } } }
why meet recycle error?
to fix outofmemory error, should this:
bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; bitmap preview_bitmap = bitmapfactory.decodestream(is, null, options);
this insamplesize option reduces memory consumption.
here's complete method. first reads image size without decoding content itself. finds best insamplesize value, should power of 2, , image decoded.
// decodes image , scales reduce memory consumption private bitmap decodefile(file f) { try { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f), null, o); // new size want scale final int required_size=70; // find correct scale value. should power of 2. int scale = 1; while(o.outwidth / scale / 2 >= required_size && o.outheight / scale / 2 >= required_size) { scale *= 2; } // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; return bitmapfactory.decodestream(new fileinputstream(f), null, o2); } catch (filenotfoundexception e) {} return null;
}
Comments
Post a Comment