memory - efficient way of loading bitmaps in android -


i'm trying load picture gallery bitmap using code:

bitmap mybitmap = bitmap.createscaledbitmap(bitmapfactory.decodefile(adress), (int)viewwidth/2, (int)viewheight/2, false); 

where 'adress' adress of picture in gallery. works fine on galaxy note 1, android version 2.3.6, crashes due 'out of memory' on galaxy 2, android version 4.1.2

am doing wrong here? there way scaled bitmap? resulting bitmap (when work) bit smudged due scaling, don't mind. thanks!!!

use method

    bitmap bm=decodesampledbitmapfrompath(src, reqwidth, reqheight); 

implementation-

     public int calculateinsamplesize(bitmapfactory.options options,         int reqwidth, int reqheight) {      final int height = options.outheight;     final int width = options.outwidth;     int insamplesize = 1;      if (height > reqheight || width > reqwidth) {         if (width > height) {             insamplesize = math.round((float) height / (float) reqheight);         } else {             insamplesize = math.round((float) width / (float) reqwidth);         }     }     return insamplesize; }  public bitmap decodesampledbitmapfrompath(string path, int reqwidth,         int reqheight) {      final bitmapfactory.options options = new bitmapfactory.options();     options.injustdecodebounds = true;     bitmapfactory.decodefile(path, options);      options.insamplesize = calculateinsamplesize(options, reqwidth,             reqheight);      // decode bitmap insamplesize set     options.injustdecodebounds = false;     bitmap bmp = bitmapfactory.decodefile(path, options);     return bmp; } 

}


Comments