2

I'm trying to alter a bitmap, but when I call these functions, I get an IllegalStateException at the line bmp.setPixel(i, j, Color.rgb(red, green, blue));

The full code for that method to alter the pixels is:

Bitmap darken(Bitmap bmp) {
    for (int i = 0; i < bmp.getWidth(); i++) {
        for (int j = 0; j < bmp.getHeight(); j++) {
            int red = (bmp.getPixel(i, j) >> 16) & 0xFF;
            int green = (bmp.getPixel(i, j) >> 8) & 0xFF;
            int blue = (bmp.getPixel(i, j) >> 0) & 0xFF;
            red -= 25.5;
            green -= 25.5;
            blue -= 25.5;
            if (red < 0) {
                red = 0;
            }
            if (green < 0) {
                green = 0;
            }
            if (blue < 0) {
                blue = 0;
            }
            bmp.setPixel(i, j, Color.rgb(red, green, blue));
        }
    }
    return bmp;
}

The method that sets imageView to the selected image is:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CAMERA && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    } else if (requestCode == REQUEST_SELECT && resultCode == Activity.RESULT_OK) {
        Uri uri = data.getData();
        try {
            Bitmap selection = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            imageView.setImageBitmap(selection);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And this is the method that runs when I hit the button:

final Button darkenButton = (Button) findViewById(R.id.darkenButton);
    darkenButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            BitmapDrawable bDraw = (BitmapDrawable) imageView.getDrawable();
            Bitmap bmap = bDraw.getBitmap();
            bmap = darken(bmap);
            imageView.setImageBitmap(bmap);
        }
    });

I'm thinking that setting the bitmap from the stored image is somehow causing a problem, because it works if I set the bitmap by taking a picture.

Stephen Burns
  • 162
  • 2
  • 17

0 Answers0