• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Creating a bitmap causes error

android88

Newbie
Jan 6, 2010
17
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:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {
            System.out.println(imageData);
            try{
                ByteArrayInputStream bytes = new ByteArrayInputStream(imageData);
//                Bitmap bm = BitmapFactory.decodeStream(bytes); #Method 1
//                BitmapDrawable bmd = new BitmapDrawable(bytes);  #Method 2
                bytes.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }    
    };
Does anyone see the problem? (if you need more code, let me know)
 
It only happens in the real phone, not in the emulator, so i don't have a logcat.

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.
 
Upvote 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:
01-11 17:54:21.600: ERROR/AndroidRuntime(3510): Uncaught handler: thread main exiting due to uncaught exception
01-11 17:54:21.600: ERROR/AndroidRuntime(3510): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:375)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:394)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at nl.uva.CameraCode.CameraCode$2.onPictureTaken(CameraCode.java:164)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:246)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.os.Looper.loop(Looper.java:123)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread.main(ActivityThread.java:3948)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at java.lang.reflect.Method.invokeNative(Native Method)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at java.lang.reflect.Method.invoke(Method.java:521)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-11 17:54:21.600: ERROR/AndroidRuntime(3510):     at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0
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:
  . . .
    Bitmap photo = null ;
    try {
      photo = BitmapFactory.decodeByteArray(data, 0, data.length) ;

      // Save the image.  This also saves a micro and mini thumbnail
      String uri = MediaStore.Images.Media.insertImage(getContentResolver(), photo, SOUNDCAM_TITLE, filename) ;
    		
      photo.recycle() ;
    } 
    catch(Exception e) {
      Log.e(TAG, "saveJPEGBitmapToMediaStore: failed to save image", e);
    } 
    finally {
      if( photo != null ) {
        photo.recycle() ;
    }
  . . .

Hope that helps.

Regards,

Mark
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones