0

my little App causes a crash when I try to set the Pixels of an Bitmap loaded by the photopicker intent and I cant figure out why. For the intent I took this code.

So here is the important part of my current code:

public class MainActivity extends AppCompatActivity {

  private Bitmap selectedImage;

  ...

  private void changebitmap() {

        ImageView imageView = findViewById(R.id.imageView);

        for(int x = 0; x < selectedImage.getWidth(); x++) {
            for(int y = 0; y < selectedImage.getHeight(); y++) {
                selectedImage.setPixel(x, y, Color.argb(255, 128, 128 ,128));
            }
        }

        imageView.setImageBitmap(selectedImage);

  }
}

I can't figure out why it won't work.

Regards

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
John Doe
  • 45
  • 6

1 Answers1

0

Ok I just got it by myself.

The Problem was that the Bitmap from the intent hadn't the right Config.

On API Level 19 it says you can just set it by

selectedImage.setConfig(Bitmap.Config.ARGB_4444);

On lower levels (like me) you can copy it pixelwise to another Bitmap with the right configuration.

selectedImage = Bitmap.createBitmap(copy.getWidth(), copy.getHeight(), Bitmap.Config.ARGB_4444);

for(int x=0; x < copy.getWidth(); x++) {
  for(int y=0; y < copy.getHeight(); y++) {

    selectedImage.setPixel(x, y, copy.getPixel(x, y));

  }
}

Regards

John Doe
  • 45
  • 6