Creating a bitmap causes error


Last Updated:

  1. android88

    android88 Member This Topic's Starter

    Joined:
    Jan 6, 2010
    Messages:
    17
    Likes Received:
    0
    Hi everyone. I'm totally new to android programming and I can't get rid of an error that occurs when running my application in my phone (the error doesn't occur in the emulator).
    I'm trying to make a picture with the camera and then convert the byte[] that the onPictureTaken function returns into a bitmap. I tried 2 approaches that both fail. So hopefully one of you sees the problem :)

    Below is the part of my code in which the problem occurs. When i just open and close the inputstream, everything works fine. But when i uncomment one of the two methods to create a bitmap, then the app crashes after taking about 3 pictures (on the fourth it gives the "application stopped unexpectedly" error). It only happens in the real phone, not in the emulator, so i don't have a logcat.

    BitmapFactory.decodeStream returns null when bytes is null, or when bytes is not a valid inputstream to create a bitmap from, so i guess that the problem can't be that bytes is null or something like that.

    Code (Text):
    1.  
    2. Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    3.         public void onPictureTaken(byte[] imageData, Camera c) {
    4.             System.out.println(imageData);
    5.             try{
    6.                 ByteArrayInputStream bytes = new ByteArrayInputStream(imageData);
    7. //                Bitmap bm = BitmapFactory.decodeStream(bytes); #Method 1
    8. //                BitmapDrawable bmd = new BitmapDrawable(bytes);  #Method 2
    9.                 bytes.close();
    10.             }catch(IOException e) {
    11.                 e.printStackTrace();
    12.             }
    13.         }    
    14.     };
    15.  
    Does anyone see the problem? (if you need more code, let me know)
     

    Advertisement
  2. KeithG

    KeithG Well-Known Member

    Joined:
    Nov 15, 2009
    Messages:
    103
    Likes Received:
    3
    Can I ask, why here? Are you trying to develop without a phone? And if your sure that .. that is where the application is dying, then you could... change the exception catch block to catch all exceptions, and then write the stack trace to a text file on the SDcard.

    But if you have a phone, there isn't any reason why you shouldn't be debugging directly on the device for these types of issues.

    Otherwise your just shooting in the dark.
     
  3. android88

    android88 Member This Topic's Starter

    Joined:
    Jan 6, 2010
    Messages:
    17
    Likes Received:
    0
    I was debugging on the real phone. I just didn't know how to see the error like in eclipse. But i just found out that i can run an application in eclipse through my phone and see the log. It turns out that i get an out of memory error. But I don't understand yet why the bitmap size is not too big for the first couple of pictures, but suddenly gets too big after that.
    Code (Text):
    1.  
    2. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510): Uncaught handler: thread main exiting due to uncaught exception
    3. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    4. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    5. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:375)
    6. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:394)
    7. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at nl.uva.CameraCode.CameraCode$2.onPictureTaken(CameraCode.java:164)
    8. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:246)
    9. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.os.Handler.dispatchMessage(Handler.java:99)
    10. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.os.Looper.loop(Looper.java:123)
    11. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread.main(ActivityThread.java:3948)
    12. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at java.lang.reflect.Method.invokeNative(Native Method)
    13. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at java.lang.reflect.Method.invoke(Method.java:521)
    14. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
    15. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
    16. 01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at dalvik.system.NativeStart.main(Native Method)
    17.  
     
  4. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi android88,

    You need to recycle your bitmap to free up the memory.
    You might get away with creating 1 or 2 bitmaps, but then you'll run out of memory.

    In my Sound Camera app I create one Bitmap, and recycle it as soon as I've finished with it. If I try to create a 2nd Bitmap then I run out of memory.

    Here's an extract:

    Code (Text):
    1.  
    2.   . . .
    3.     Bitmap photo = null ;
    4.     try {
    5.       photo = BitmapFactory.decodeByteArray(data, 0, data.length) ;
    6.  
    7.       // Save the image.  This also saves a micro and mini thumbnail
    8.       String uri = MediaStore.Images.Media.insertImage(getContentResolver(), photo, SOUNDCAM_TITLE, filename) ;
    9.            
    10.       photo.recycle() ;
    11.     }
    12.     catch(Exception e) {
    13.       Log.e(TAG, "saveJPEGBitmapToMediaStore: failed to save image", e);
    14.     }
    15.     finally {
    16.       if( photo != null ) {
    17.         photo.recycle() ;
    18.     }
    19.   . . .
    20.  
    Hope that helps.

    Regards,

    Mark
     
  5. android88

    android88 Member This Topic's Starter

    Joined:
    Jan 6, 2010
    Messages:
    17
    Likes Received:
    0
    Thanks alot. That was indeed the problem :)
     

Share This Page

Loading...