-1

I am setting this bitmap on canvas and getting this IllegalArgumentException while using getPixels() method:

java.lang.IllegalArgumentException: x + width must be <= bitmap.width() in the line

bitmap.getPixels(pixels, 0,bitmap.getWidth(), 1, 1, bitmap.getWidth(), bitmap.getHeight());

How to fix this?

Kirill
  • 7,580
  • 6
  • 44
  • 95
zek54
  • 415
  • 3
  • 20

1 Answers1

2

the exception already tells you your error

x + width must be <= bitmap.width()

your x and y start at 1 instead of 0, so the method calculates x + width = (1+ width) which is out of bounds. set your x and y values to 0 or if you really want to skip the first pixel in each row and column set your bitmap.getWidth and bitmap.getHeight() to "bitmap.getWidth-1" and "bitmap.getHeight()-1".

for a better explanation see the doc

https://developer.android.com/reference/android/graphics/Bitmap.html#getPixels(int[], int, int, int, int, int, int)

Senbazuru
  • 381
  • 3
  • 14