Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalArgumentException in Bitmap.getPixels()

I'd like to copy the data from a Bitmap into an int[] using getPixels(), this is my current code:

int[] pixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0,
        myBitmap.getHeight(), myBitmap.getWidth());

for(int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
    Log.e(TAG, "pixel"+i+"" +pixels[i]);
}

But it's throwing an exception:

05-04 20:24:08.281: ERROR/AndroidRuntime(5700): Uncaught handler: thread main exiting due to uncaught exception
05-04 20:24:08.296: ERROR/AndroidRuntime(5700): java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
05-04 20:24:08.296: ERROR/AndroidRuntime(5700):     at android.graphics.Bitmap.checkPixelsAccess(Bitmap.java:818)
05-04 20:24:08.296: ERROR/AndroidRuntime(5700):     at android.graphics.Bitmap.getPixels(Bitmap.java:771)
05-04 20:24:08.296: ERROR/AndroidRuntime(5700):     at com.tecmark.Jjilapp$TouchView.onDraw(Jjilapp.java:206)

Any ideas? I just specified the y param as 0.

like image 844
turtleboy Avatar asked May 04 '11 19:05

turtleboy


1 Answers

I think there's a typo in this code. Try this:

myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0,
   myBitmap.getWidth(), myBitmap.getHeight());
like image 93
Michael Avatar answered Oct 19 '22 04:10

Michael