1

I know this is a very frequently asked questions and there are multiple answers. My question is "which is better?" I've an activity where I capture an image using camera. I need to pass this image to another activity. One way is to create Bitmap and pass it in putExtra since Bitmap is a Parcelable. This fails when the image size is too big.

I found 2 solutions. This answer uses MemoryCache to save and retrieve the image. Many answers (this, this and this) recommend to save the image to storage and then pass the path to new activity and read the image there.
Which is a better method in this case? (In terms of speed and memory)

Nagabhushan S N
  • 6,407
  • 8
  • 44
  • 87

1 Answers1

1

It is safer in any case work with path or link than pass it as it is. It works not only with images but with most types of data. At the same time while working with path you can easy make if/else checks and handling some unexpected scenarios. Also pass the path is much faster.

Bo Z
  • 2,359
  • 1
  • 13
  • 31
  • So, you're saying Saving file is better than using MemoryCache. RIght? – Nagabhushan S N Apr 24 '19 at 17:48
  • @NagabhushanSN the question was what is the better way to pass between activity. So most common practice is working with path. Try to imagine that other class need to work with the same image other developer in your team also need to handle that image somehow. By using path to the file you can use / modify / delete it easy from any part of the app. – Bo Z Apr 24 '19 at 19:23
  • Okay got it. Suppose if I'm handling everything, in that case which is better? In terms of speed (highly important) and memory as well. – Nagabhushan S N Apr 24 '19 at 19:32
  • @NagabhushanSN cache is better i guess in your case. I don't know what exactly you doing in your app I might have other logical solution but if we choosing between two and you have heavy images so first you need to hadle them to avoid problems with productivity such as memory leak and cache could help you with that and after that use however you need. How to cache it you can find here https://developer.android.com/topic/performance/graphics/cache-bitmap – Bo Z Apr 24 '19 at 19:41
  • I have a CameraActivity. I take a pic there and then pass it to next activity which displays image and performs some editing, processing and stuff. – Nagabhushan S N Apr 24 '19 at 19:49
  • 1
    @NagabhushanSN So you could simply handle image with help of the cache after that put the path into extras and send intent to other activity --> other activity while have the path (dont forget to check for null and handle errors) extract that image and edit. Thats the example how to do that https://stackoverflow.com/questions/29717038/android-saving-image-to-storage – Bo Z Apr 24 '19 at 19:57