0

I get camera frames, do some processing using OpenCV, and generate several Bitmap images. I want to pass them to another activity. Now, these images are not very small. In fact, if I try to pass all of them using intent.putExtra as parcelables, I get

TransactionTooLargeException

In case you ask, these images are not resources. They are purely generated images and are in memory. So there is no URI to find them.

What is the best way to solve this problem?

M. Azyoksul
  • 1,580
  • 2
  • 16
  • 43
  • You need to convert it to byteArray I guess – esQmo_ Aug 06 '20 at 23:30
  • Will converting it to byte array prevent TransactionTooLargeException @esQmo_? – M. Azyoksul Aug 06 '20 at 23:32
  • Yes since passing with parcelable would fail because of the size of parcelable (1mb I guess). So there are couple of way to achieve this among those converting to byteArray, saving the bitmap in internal storage, see: https://stackoverflow.com/a/11010565/5374691 – esQmo_ Aug 06 '20 at 23:41
  • Does this answer your question? [Passing android Bitmap Data within activity using Intent in Android](https://stackoverflow.com/questions/11010386/passing-android-bitmap-data-within-activity-using-intent-in-android) – Javad Dehban Aug 07 '20 at 04:31

2 Answers2

1

You need first to convert it to byteArray

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

Intent i = new Intent(this, Activity.class);
i.putExtra("bitmap", byteArray);
startActivity(i);

And retrieve it like:

Bitmap bitmap;
//...

byte[] byteArray = getIntent().getByteArrayExtra("bitmap");
bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

You can also read the documentation here

esQmo_
  • 1,464
  • 3
  • 18
  • 43
0

I suggest you save these images to internal memory using openFileOutput and pass saved filenames to the next activity where they will be retrieved via openFileInput.

Because as @esQmo_ mentioned, bundles are very limited in memory size and are not designed to transfer a large amount of data.

Veniamin
  • 774
  • 5
  • 12