java - Decoding File from Disc Cache into Bitmap referencing to same Bitmap Object -
i trying load added bitmap disc cache recyclerview. processed bitmap disc cache returning different image referencing same bitmap object. hence of imageview in recyclerview change processed bitmap. image processing code given below
/** * disk cache. * * @param data unique identifier item * @return bitmap if found in cache, null otherwise */ public bitmap getbitmapfromdiskcache(string data) { //begin_include(get_bitmap_from_disk_cache) final string key = hashkeyfordisk(data); bitmap bitmap = null; synchronized (mdiskcachelock) { while (mdiskcachestarting) { try { mdiskcachelock.wait(); } catch (interruptedexception e) { } } if (mdisklrucache != null) { inputstream inputstream = null; try { final disklrucache.snapshot snapshot = mdisklrucache.get(key); if (snapshot != null) { if (buildconfig.debug) { log.d(tag, "disk cache hit"); } inputstream = snapshot.getinputstream(disk_cache_index); if (inputstream != null) { filedescriptor fd = ((fileinputstream) inputstream).getfd(); log.d("disccache", "file=" + inputstream); // decode bitmap, don't want sample give // max_value target dimensions bitmap = imageresizer.decodesampledbitmapfromdescriptor( fd, integer.max_value, integer.max_value, this); } } } catch (final ioexception e) { log.e(tag, "getbitmapfromdiskcache - " + e); } { try { if (inputstream != null) { inputstream.close(); } } catch (ioexception e) { } } } return bitmap; } //end_include(get_bitmap_from_disk_cache) }
method imageresizer
public static bitmap decodesampledbitmapfromdescriptor( filedescriptor filedescriptor, int reqwidth, int reqheight, imagecache cache) { // first decode injustdecodebounds=true check dimensions final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefiledescriptor(filedescriptor, null, options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); // decode bitmap insamplesize set options.injustdecodebounds = false; // if we're running on honeycomb or newer, try use inbitmap if (utils.hashoneycomb()) { addinbitmapoptions(options, cache); } return bitmapfactory.decodefiledescriptor(filedescriptor, null, options); } public static int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) { // begin_include (calculate_sample_size) // raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { final int halfheight = height / 2; final int halfwidth = width / 2; // calculate largest insamplesize value power of 2 , keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } // offers additional logic in case image has strange // aspect ratio. example, panorama may have larger // width height. in these cases total pixels might still // end being large fit comfortably in memory, should // more aggressive sample down image (=larger insamplesize). long totalpixels = width * height / insamplesize; // more 2x requested pixels we'll sample down further final long totalreqpixelscap = reqwidth * reqheight * 2; while (totalpixels > totalreqpixelscap) { insamplesize *= 2; totalpixels /= 2; } } log.d(tag, "h=" + height + "rh=" + reqheight + "w=" + width + "rw=" + reqwidth + "sz=" + insamplesize); return insamplesize; // end_include (calculate_sample_size) }
Comments
Post a Comment