1. Are you ready for the Galaxy S20? Here is everything we know so far!

Creating a bitmap causes error

Discussion in 'Android Development' started by android88, Jan 11, 2010.

  1. android88

    android88 Newbie
    Thread Starter

    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)
     


  2. KeithG

    KeithG Well-Known Member

    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 Newbie
    Thread Starter

    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

    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 Newbie
    Thread Starter

    Thanks alot. That was indeed the problem :)
     
Loading...
Similar Threads - Creating bitmap causes
  1. MoodyBlues
    Replies:
    0
    Views:
    56
  2. harrytapp
    Replies:
    0
    Views:
    109
  3. mdsousa
    Replies:
    0
    Views:
    295
  4. mdsousa
    Replies:
    3
    Views:
    359
  5. Stokestack
    Replies:
    0
    Views:
    575
  6. TheOuz
    Replies:
    0
    Views:
    486
  7. raccoon95
    Replies:
    0
    Views:
    419
  8. monInspiration
    Replies:
    0
    Views:
    547
  9. James Mountain
    Replies:
    1
    Views:
    516
  10. Rob
    Replies:
    0
    Views:
    316

Share This Page

Loading...