0

I need to save a captured image from camera on a tmp file and retrieve it on the next activity, so i can handle better the OOM error.

So i did something like this to write the file:

public void savePicAtFile(Bitmap pic){
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("tmpCameraPic");
            pic.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
            // PNG is a lossless format, the compression factor (100) is ignored
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

now i don't know how to retrieve the compressed image, should i just create a bitmap and getall the stream?

Any help?

afcosta007
  • 93
  • 7
  • "I need to save a captured image from camera on a tmp file and retrieve it on the next activity, so i can handle better the OOM error" -- this will *increase* the chances of an `OutOfMemoryError`, not decrease it. – CommonsWare Jun 29 '17 at 17:33
  • can you explain me why? what is the best way to send a bitmap then? someone said to me to do it that way because it is google best practice :S – afcosta007 Jun 29 '17 at 17:35
  • found something, then why this: https://stackoverflow.com/questions/11010386/send-bitmap-using-intent-android? – afcosta007 Jun 29 '17 at 17:38
  • "can you explain me why?" -- because now you have two huge `Bitmap` objects instead of one. "what is the best way to send a bitmap then?" -- the best way is to not have two activities here, so you are not passing a `Bitmap` between activities. Instead, have one activity with multiple fragments or something. Next-best is to *very carefully* use a `static` field, so that both activities can have access to the same `BItmap`. Just bear in mind that this `Bitmap` memory cannot be garbage-collected until you `null` out the `static` reference to it. – CommonsWare Jun 29 '17 at 17:39
  • as long as i can't change all my project to use fragments, or this module, can you explain me how you do it with the static? and the garbage-collector? – afcosta007 Jun 29 '17 at 17:49
  • @CommonsWare I disagree that a static is appropriate. If you need to share images between activities, you're better off using an LRUCache or similar functionality which you can memory cap and clear in onLowMemory(). Static bitmaps are just asking for problems. – Gabe Sechan Jun 29 '17 at 17:51
  • well nice that i asked this, because all the answers are so diferent about this topic, Gabe can you explain better about the LRUCache? – afcosta007 Jun 29 '17 at 17:52
  • @GabeSechan: In this situation, I am skeptical that it adds value. With a `static` `LRUCache`, you could accidentally cache N bitmaps; with a single `static` field, you could only cache 1. And both can be handled via `onLowMemory()` equally well. Personally, I am more far more a fan of "neither of those" and having this be one activity rather than two. – CommonsWare Jun 29 '17 at 18:08
  • 'need to save a captured image from camera on a tmp file and retrieve it on the next activity' ?? Where did you get that bitmap from to begin with? If you capture an image with the camera you will not get a bitmap mostly. Further if you have managed to create a file for an image then pass the file path to the next activity. Then that activity can do than with the file what it wants. – greenapps Jun 30 '17 at 00:57

0 Answers0