Q:

Android change raw PNG data

I have a PNG file in my project where I want to change some values at run time.
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try {
        InputStream input = getIntro().getAssets().open("image.png");
        byte[] tmp = new byte[1024];
        int ret = 0;
        while ((ret = input.read(tmp, 0, 1024)) >= 0) {
            output.write(tmp, 0, ret);
        }
    } catch (IOException ex) {
        System.out.print(ex);
    }

    byte[] imgArray = output.toByteArray();

    imgArray[1000] = (byte) Color.red(Const.SOMEVALUE);

    return BitmapFactory.decodeByteArray(imgArray, 0, imgArray.length);

whatever I do in imgArray[1000] = (byte) Color.red(MyApplication.COLOR_BOARD_BG) line, I get an empty image. If I don't use that line and modify byte array manually it's OK, however anything I change (in the header or body) does not differ = blank image.
Actually I try to modify the palette information but here as an example I change the 1000th element which is some value within image data.

A:

The PNG fileformat stores the image data in compressed form. Changing just one byte somewhere probably invalidates an internal checksum, resulting in an invalid image, so you can't do that. In addition to that, each PNG chunk is protected by a 32-bit CRC checksum, so any modification to any part of the file requires updating at least the checksum to the chunk you modify
If you want to modify the pixels of the image it will be easier to decode the PNG data first, using libraries that are available to you, like BitmapFactory and Bitmap on Android. 
Bitmap png = BitmapFactory.decodeFile(selectedImagePath);
png.setPixel(34,43,0xFFFF0000);

