0

I have been trying to pass a single byte array (compressed bitmap) from one activity to another. When I attempt to decode the byte array back to a bitmap, and show the bitmap, it appears to be a completely transparent bitmap.

I use this code on the first activity to compress the bitmap and send the byte array:

try {
    InputStream selectedImage = getContentResolver().openInputStream(Uri.parse(photoPath));
    bitmap = BitmapFactory.decodeStream(selectedImage);
} catch (FileNotFoundException exception) {
    Log.e(this.toString(), exception.toString());
}

ByteArrayOutputStream stream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte [] byteArray = stream.toByteArray();

Log.e("PANTHERIT_BYTEARRAY", byteArray.toString());

Intent stickerActivity = new Intent (this, StickerActivity.class);
stickerActivity.putExtra("byteArray", byteArray);
startActivity(stickerActivity);

And I use this code in the accepting activity to decompress and store the bitmap:

byte [] byteArray = getIntent().getByteArrayExtra("byteArray");

Log.e("STICKER_BYTEARRAY", byteArray.toString());

BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);

ImageView bitmapview = (ImageView) findViewById(R.id.bitmap_view);
bitmapview.setImageBitmap(bitmap);

I copied the code to the first activity, compressed the bitmap and immediately decompressed it. I took the decompressed bitmap and set it to an ImageView in that activity and it worked fine. So the error seems to be in the passing of the byte array from the first activity to the second, but I cannot figure out why that would be.

andrewdleach
  • 2,458
  • 2
  • 17
  • 25
Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Did you verify the same data comes out of the intent that you put in? If so then the problem must be how you are loading it in the ImageView. Is there a chance the view hasn't inflated completely yet? Is there a chance the decodeByteArray() method is changing the data some how and you need to provide more/different options? – Hardy Jul 22 '15 at 21:12
  • @Hardy I'm not sure if I checked it correctly, but it would seem the data that comes out of the intent is different than what goes in. I don't think the problem is with decodeByteArray() or how I am loading it into the ImageView, considering when I tested them out without passing through the intent, it worked fine. – Bryan Jul 23 '15 at 12:25

1 Answers1

0

I found an answer. I do not really know why the byte array was getting messed up, maybe it was too large? Even so, both the input and output byte arrays were the same length, so not a very good explanation.

Anyway, I found my answer here: Send Bitmap Using Intent Android. The first answer by Zaid Daghestani is the same as what I initially tried, but his edit is what worked. Instead of passing a byte array, you save the compressed bitmap to a temporary file, and pass the filename through the intent. Definitely works for passing a bitmap, now I just have to figure out how to display the bitmap on a SurfaceView instead of an ImageView...

Also, just in case you need a mutable bitmap (in order to pass it to an ImageView) you can use:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
bitmap = BitmapFactory.decodeStream(input, null, options);
Community
  • 1
  • 1
Bryan
  • 14,756
  • 10
  • 70
  • 125