0

In my code I have my own camera and it works and saves the pictures properly. First I call:

  myCamera.takePicture(null, null, jpegCallBack);

Then I want to save the picture and display it in the same activity I'm now. So in the jpegCallBack I have (fullPic is my ImageView):

  try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();                                            


            Media.getBitmap(getContentResolver(), Uri.fromFile(pictureFile) );
            String image_string = Uri.fromFile(pictureFile).toString();
            bm = BitmapFactory.decodeFile(image_string);
            fullPic.setImageBitmap(bm);                 
        } catch ...

But it doesn't work, the first time I press the Shot Button, it saves the file and continues displaying the camera. Sometimes when I take the second one app stops.

I've also tried with this:

  fullPic.setImageURI(Uri.fromFile(pictureFile));

and it says: "Bitmap too large to be uploaded into a texture (4608x2592, max=4096x4096)"

I can reduce the size of the displayed image if it is necessary (but not the saved one), but I don't know how to do it.

In the AndroidManifest I have permission for both WRITE and READ. My XML is like this:

  <LL>
     <RL>
       <FrameLayout></FL> for the Camera Preview 
       <Button></> To shot the picture
       <ImageView></> Here I want to show the picture taken
     </RL>
  </LL>

Why doesn't it work? Any ideas to display it? I have been looking another stackoverflow links but I don't find the solution. If you need more data/code, please, let me know, I tried to summarize to make it the clearest possible.

Thank you!

Edit:

This is the call of jpegCallBack:

  private PictureCallback jpegCallBack = new PictureCallback() {

      @Override
      public void onPictureTaken(byte[] data, Camera camera) 

Maybe I can so the image directly from that data in some way. Can I? I tried this way but it breaks:

bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            fullPic.setImageBitmap(bm);

LogCat: On the blue line I press the shot button

enter image description here

NullPointerException, is it possible that it occurs because I'm using a Samsung? In this thread they say that Samsung has problems with this: Capture Image from Camera and Display in Activity

Community
  • 1
  • 1
serfe
  • 285
  • 5
  • 13

1 Answers1

1

You're not getting NullPointerException, but out of memory error.

There is a series of very helpful articles on how to load bitmap efficiently. The only thing you need to know is the target display size so you will know to which size to scale down.

If the app is crashing on a 5MB allocation it means you are already at the peek of your memory usage. Please check this Android presentation on memory management.

gunar
  • 14,660
  • 7
  • 56
  • 87
  • Thanks, I'm going to read them, I cannot see one hour video right now. – serfe Sep 26 '13 at 11:32
  • From experience, fixing out of memory issues is not something that can be fixed in 10 mins ... – gunar Sep 26 '13 at 11:34
  • (I know, I can spend all day long reading or whatever, just I cannot see videos now, other issues) – serfe Sep 26 '13 at 11:38
  • That bitmap has no id ... you can identify it only by URI (you should know where the bitmap is saved). Or you can specify explicitly where to store it (I am always doing it). And then you could [use this](http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String)) (convert URI to path as string) – gunar Sep 26 '13 at 11:42
  • But I cannot use the same function because it asks for an int? Isn't it? - BitmapFactory.decodeResource(getResources(), R.id.myimage, options); (I don't know why the other message is deleted) – serfe Sep 26 '13 at 11:44
  • I know where I save the image, I can get the URI, but I don't know what to put insted of the id, since it doesn't accept other that int values. – serfe Sep 26 '13 at 11:46
  • The url I gave you in my other comment shows an overloaded method that takes as input a path (String). Again, [the link](http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String)). The only thing you need is to figure out how to convert the URI to a correct String path. As there are 2 types of URI: `file` and `content`. – gunar Sep 26 '13 at 11:51
  • Oh, I didn't see the second method, sorry. And thank you very much for your help! It works, it shows the pic, now I must work to adapt it to my requeriments. Thanks again. – serfe Sep 26 '13 at 12:10