Android getPixels (), maybe a stupid mistake?

Well, this is pretty easy to understand, but for some bizarre reason, I can't get it to work. I simplified this example from the actual code.

InputStream is = context.getResources().openRawResource(R.raw.someimage); Bitmap bitmap = BitmapFactory.decodeStream(is); try { int[] pixels = new int[32*32]; bitmap.getPixels(pixels, 0, 800, 0, 0, 32, 32); } catch(ArrayIndexOutOfBoundsException ex) { Log.e("testing", "ArrayIndexOutOfBoundsException", ex); } 

Why do I keep getting an ArrayIndexOutOfBoundsException? an array of 32x32 pixels, and as far as I know, I use getPixels correctly. Image sizes are 800x800, and I'm trying to get a 32x32 partition. The image is a 32-bit PNG that is reported as ARGB-8888.

Any ideas? even if I'm an idiot! I'm going to throw the keyboard out of the window: D

+4
source share
3 answers

You overflow the destination buffer because you are requesting a step of 800 entries between lines.

http://developer.android.com/reference/android/graphics/Bitmap.html#getPixels%28int [],% 20int,% 20int,% 20int,% 20int,% 20int,% 20int% 29

+4
source

use bitmap width as step, in case of ur 32

  bitmap.getPixels(pixels, 0, 32, 0, 0, 32, 32); 

every space in a line with 800 reasons ur pixelarray to exit the bound

"I'm going to throw the keyboard out of the window" funny lol

+9
source

You get an OutOfBounds exception. becacuse stride is not applied to the pixel array to the original bitmap, so in your case you are trying to get 32 ​​* 800 pixels that do not fit into your array.

+3
source

All Articles