Saturday, August 24, 2013

Re: [android-developers] Reusing bitmaps for BitmapFactory.decodeFile and BitmapFactory.Options.inBitmap

Here is what the documentation says: "The current implementation necessitates that the reused bitmap be mutable and of the same size as the source content."

Make sure the bitmap you are reusing is mutable.


On Sat, Aug 24, 2013 at 12:07 AM, danik <danikula@gmail.com> wrote:
My app decodes a lot of bitmaps from sd card, so I want to reuse existing bitmaps to decrease GC work. I see examples from Android Training and this video and it works perfect forBitmapFactory.decodeResource, but not for BitmapFactory.decodeFile. I 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 as expected, it decodes new bitmap using existing bitmap. But uncommented code (BitmapFactory.decodeFile) doesn't decode new bitmap. It just writes to log message "E/BitmapFactory: Unable to decode stream: java.lang.IllegalArgumentException: Problem decoding into existing bitmap"

So where is my mistake?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Romain Guy
Android framework engineer
romainguy@android.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment