android - How to efficiently decode a image knowing only its path -
i'm creating own gallery using viewpager, 'connected' arraylist holding paths of images display.
so far, use:
bitmap bitmap = bitmapfactory.decodefile(path); imageview.setimagebitmap(bitmap); imageview.setscaletype(imageview.scaletype.center_inside);
it's working, pictures pretty big , unnecessarily decode whole picture, because it's scaled down later fit layout. slow , crash outofmemoryerror happens sometimes, when scroll fast.
so question is: how can efficiently decode image fits 100% imageview.scaletype.center_inside knowing path?
do not decode directy. use injustdecodebounds = true
bitmapfactory.options
, load width , height bitmap. can calcuate insamplesize
scale bitmap
bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(path, options);
decodefile return null , options contains width
, height
. calculate insamplesize
and:
bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = false; options.insamplesize = myinsamplesize; bitmap output = bitmapfactory.decodefile(path, options);
Comments
Post a Comment