0

I'm loading my bitmaps with preferred config RGB565 from my assets folder and logging their format config but i'm always getting ARGB8888.I'm sure about some of my bitmap's config is RGB565 but i think they covers an area like an ARGB8888 bitmap.How can i load them with their own formats?

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(in,null,options);
Log.d("a", fileName+" "+bitmap.getConfig().name());
droidmachine
  • 577
  • 2
  • 8
  • 24

1 Answers1

1

You wont always get RGB565 that way - it depends on the encoding (its a preferred encoding)

After loading the bitmap you can create a new one and copy it to there... as you are effectively re-encoding the image.

Bitmap maskBitmap = Bitmap.createBitmap( orig.getWidth(), orig.getHeight(), Bitmap.Config.RGB_565 );
Canvas c = new Canvas();
c.setBitmap(maskBitmap);
Paint p = new Paint();
p.setFilterBitmap(true); // possibly not nessecary as there is no scaling
c.drawBitmap(orig,0,0,p);
orig.recycle();

Then you can use maskBitmap as the loaded bitmap.

siliconeagle
  • 7,379
  • 3
  • 29
  • 41
  • Thank you but isn't that will increase loading time very much? – droidmachine Aug 27 '12 at 19:43
  • @droidmachine and increasing the memory usage. Which is VERY BAD. – IAmGroot Aug 27 '12 at 20:12
  • 1
    @Doomsknight Okey but isn't there a solution for loading bitmaps as rgb565?Actually my purpose is using this bitmap with opengl's glTex2D function and reduce memory use.What can i do about that? – droidmachine Aug 27 '12 at 20:18
  • @siliconeagle I didn't marked down.I just removed the accepted answer check. I just want to use RGB565 bitmaps when ARGB8888 is not needed.How can i do this? – droidmachine Aug 27 '12 at 20:50
  • thats what this code does. You have to CONVERT THE IMAGE. Simialr the scaling it - you have to allocate the new piece of memory and the copy the re-encoded pixels into it. Thats how you do it. Yes it uses more memory while you are loading bitmaps but after that the ARGB8888 bitmaps are de-allocated(orig.recycle()) and the remaining bitmap is RGB565. – siliconeagle Aug 27 '12 at 20:56
  • it does sound contra-logical but whenever you convert an image you have to allocate another bitmap to hold the result. – siliconeagle Aug 27 '12 at 20:57
  • @siliconeagle So where is the memory problem like Doomsknight said? recycle() de-allocate memory and removes bitmap from memory so there isn't any leak.I just want to convert it and use with glTex2D.After that i'm recycling all the bitmaps already. – droidmachine Aug 27 '12 at 21:03
  • Its not a leak, its having two bitmaps in memory at the same time thats the problem. Even if it is a short while. Bitmaps can be huge in size, and just one is capiable of Out of memory. Two just doubles the chance. If you follow this info to keep bitmap size small, you should be ok. http://developer.android.com/training/displaying-bitmaps/index.html Im surprised your original code doesn't work. – IAmGroot Aug 27 '12 at 23:13