android - Reusing bitmaps for BitmapFactory.decodeFile and BitmapFactory.Options.inBitmap -
my app decodes lot of bitmaps sd card, want reuse existing bitmaps decrease gc work. see examples android training , this video , works perfect bitmapfactory.decoderesource, not bitmapfactory.decodefile. use next code:
private void testbitmapreusing() { bitmapfactory.options options = newoptions(); bitmap bitmap = decode(options); options.inbitmap = bitmap; bitmap = decode(options); } private bitmapfactory.options newoptions() { bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 1; options.inmutable = true; return options; } private bitmap decode(bitmapfactory.options options) { return bitmapfactory.decodefile("/mnt/sdcard/sun.jpg", options); // return bitmapfactory.decoderesource(getresources(), r.drawable.sun, options); }
commented code (bitmapfactory.decoderesource
) works expected, decodes new bitmap using existing bitmap. uncommented code (bitmapfactory.decodefile
) doesn't decode new bitmap. writes log message "e/bitmapfactory: unable decode stream: java.lang.illegalargumentexception: problem decoding existing bitmap"
so mistake?
update realize fail. try decode gif images, impossible reuse bitmaps in gif format. docs says:
the source content must in jpeg or png format (whether resource or stream)
it might not you're looking directly, alternate approach create map (e.g. hashmap) , add resources they're being called. this:
hashmap<string, bitmap> map = new hashmap<string, bitmap>(); public bitmap loadbitmap(string location) { if (map.containskey(location)) return map.get(location); bitmap bmp = yourdecodemethod(location); map.put(location, bmp); return bmp; }
this same way use when not want reload resources every time. sorry if not wanted do, tried best understand actual goal :)
good luck!
Comments
Post a Comment