EDIT : I've included more of my actual code for clarity.
Consider this example code snippet
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etext = findViewById(R.id.editText2);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
try {
bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
bmp=bmp.copy(Bitmap.Config.ARGB_8888 , true);
encode(bmp);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void encode(Bitmap bm)
{
int height = bm.getHeight();
int width = bm.getWidth();
int pixels[] = new int[height * width];
bm.getPixels(pixels, 0, width, 0, 0, width, height);
pixels[pixels.length - 1] = (byte) 1;
pixels[pixels.length - 2] = (byte) 2;
pixels[pixels.length - 3] = (byte) 3;
bm.setPixels(pixels, 0, width, 0, 0, width, height);
}
When I put output statements before setPixels()
, I get correct values - the values that I want to store.
After setPixels()
, in another part of the code I'm reading the same image. Over there when I read the values from indices I've modified, I get completely different values. The values I stored are overwritten.
I have tried both - typecasting to byte and not. At multiple places. Didn't solve my issue.
The values I'm storing at those indices are in the range 0-127, ASCII range. Well within the byte range.