5

I'm trying to read in a .jpg using BitmapFactory. I want to get a RGB888 formatted bitmap, but I keep seeming to get RGB565. Is there anyway to change this?

Jay
  • 53
  • 1
  • 3

1 Answers1

4

BitmapFactory methods let you pass a BitmapFactory.Options instance. You can use BitmapFactory.Options to specifcy the inPreferredConfig, which defines the format of the Bitmap returned after decoding, just set it to Bitmap.Config.ARGB_8888.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 1
    Ok. I seem to be having a problem. I try something like: BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bmp = BitmapFactory.decodeFile("/path/to/file.jpg", opts); It crashes for some reason. – Jay Feb 01 '11 at 19:16
  • It seems that it doesn't like ARGB_8888. I take that line out and it's fine. – Jay Feb 01 '11 at 19:47
  • What is the stack trace from the crash? What's the size of the Bitmap you're trying to decode? If it's crashing when you use ARGB_8888 but not otherwise, my first inclination would be to think that the Bitmap is large, and takes up too much memory as an ARGB_8888 (32bpp) but not as RGB_565(16bpp). – Jason LeBrun Feb 01 '11 at 19:55
  • I don't know how to get the stack trace. But the image is a 1944 by 2592 jpg. – Jay Feb 01 '11 at 20:07
  • To get the stack trace, use the logcat tool that comes with the SDK. You can use it from within Eclipse, see the docs. – Jason LeBrun Feb 01 '11 at 20:20
  • However, without the logcat, I can already tell you that the image is too large. 1944*2592*32bpp = 20.1MB, almost certainly too much to fit on the heap. – Jason LeBrun Feb 01 '11 at 20:21
  • Damn. Ok. Thanks. I'll see if I can figure out another way to work this then. Is jpeglib or libpng ported to android by chance? – Jay Feb 01 '11 at 20:22
  • Do you really need to load the image at full resolution? It's not possible to show it all in the screen. Investigate the BitmapFactory.Options.inSampleSize parameter, which can help you to load the bitmap in a way that requires less memory. – Jason LeBrun Feb 01 '11 at 20:23
  • Or is there a way to buffer the data and do it in parts? – Jay Feb 01 '11 at 20:23
  • Unfortunately, there's no way to load a subsection of a bitmap using the BitmapFactory methods. Some other SO users are investigating this issue as well, there is more discussion in this question: http://stackoverflow.com/questions/4815192/is-it-possible-to-chop-a-bitmap-to-small-pieces-without-loading-the-entire-thing – Jason LeBrun Feb 01 '11 at 20:24
  • See my answer at http://stackoverflow.com/questions/4815192/is-it-possible-to-chop-a-bitmap-to-small-pieces-without-loading-the-entire-thing – Romain Guy Feb 04 '11 at 22:08
  • isn't ARGB_8888, 32bit format??? And it is advised not use it as it uses more memory..... – jxgn Sep 21 '12 at 08:34
  • ARGB_8888 is 32 bits and I would not advise against using it. The gain in quality is just too important. – Romain Guy Sep 24 '12 at 17:05