0

I am trying to create Android CameraX app where user can take a photo, apply some image processing effects and view the result, without a need to save it to disk. But for me, the most challenging part is to pass the image to another activity where it will be processed.

  1. I could create an Intent and attach the image using PutExtra(). But it would take too much memory and slow down the UI, since intent extras are not recommended to exceed 1 Mbyte. For example, 8MP photo would consume 24 Mb uncompressed and about 1.3 Mb in JPEG format.
  2. Share the image in public class properties - according to Android docs, it's not a reliable way to pass data directly between activities, because the system may create and destroy them at any time.
  3. Okay, let's capture the image into a file to and open it in our second activity. Or just call a built-in camera app to do so. It wouldn't be a problem for couple of images. But if user wants to capture 20-30 of them and more, it will cram the media folder and wear down the device storage.

Which is the best practice to pass image data between activities?

iHs
  • 1
  • 1

2 Answers2

0

Store the image in your local storage and pass the URI of it as intent between activities because the ram is more costly than storage. This is a way how WhatsApp has exposed its intent to pass the image.

Also regarding multiple captures, you can easily handle it through code in your app.

Helpful Link

royatirek
  • 2,437
  • 2
  • 20
  • 34
0

I have found a quite simple way: if I don't need more than one instance of certain data, I can declare an application level object (right click in java folder - new - kotlin file - enter name and choose "object"). It is initialised on first access, and stores its properties throughout the application's life. It may also contain methods to read/write/process the data. In onCaptureSuccess(img: ImageProxy) listener I obtain the image from ImageProxy (it's in JPEG format), decode it with BitmapFactory and assign the Bitmap to object field. In other activity, all to do is to read the Bitmap from the object.

iHs
  • 1
  • 1