更改位图时出现IllegalStateException

时间:2017-07-25 00:49:18

标签: android bitmap imageview

我正在尝试更改位图,但是当我调用这些函数时,我在行bmp.setPixel(i, j, Color.rgb(red, green, blue));

处收到IllegalStateException

改变像素的方法的完整代码是:

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;
}

将imageView设置为所选图像的方法是:

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();
        }
    }
}

这是按下按钮时运行的方法:

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);
        }
    });

我认为从存储的图像设置位图会以某种方式导致问题,因为如果我通过拍照来设置位图,它就会起作用。

0 个答案:

没有答案