2

I am getting an out of memory trying to take a picture with the CWAC library in android. I am just using a button in the main activity and creating the fragment on the fly.

The preview will come up fine. But when i click the take picture it will crash with out of memory. Can someone suggest how to fix this problem?

CameraFragment cameraFragment = new CameraFragment();

        setContentView(R.layout.activity_shopping);
        //Create the CameraFragment and add it to the layout
        //CameraFragment f = new CameraFragment();
        getFragmentManager().beginTransaction()
                .add(R.id.container, cameraFragment, TAG_CAMERA_FRAGMENT)
                .commit();

        //Set the CameraHost
        SimpleCameraHost.Builder builder=
                new SimpleCameraHost.Builder(new SimpleCameraHost(this));

        //SimpleCameraHost simpleCameraHost = new SimpleCameraHost(this);
        cameraFragment.setHost(builder.useFullBleedPreview(true).build());

        takePicture = (Button) findViewById(R.id.buttonPicture);

        //Set an onClickListener for a shutter button
        findViewById(R.id.buttonPicture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                takePicture();
            }
        });

private void takePicture() {
    CameraFragment f = (CameraFragment) getFragmentManager().findFragmentByTag(TAG_CAMERA_FRAGMENT);
    if (f != null && f.isVisible()) {
        PictureTransaction xact=new PictureTransaction(f.getHost());

    xact.flashMode(Camera.Parameters.FLASH_MODE_AUTO);
    f.takePicture(xact);
    }
}

06-30 15:32:10.661  29474-29823/com.aithops.provrum.app E/dalvikvm-heap﹕ Out of memory on a 31961104-byte allocation.
06-30 15:32:10.661  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ "Thread-32873" prio=5 tid=12 RUNNABLE
06-30 15:32:10.661  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x425fcb40 self=0x5976d688
06-30 15:32:10.661  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ | sysTid=29823 nice=0 sched=0/0 cgrp=apps handle=1501887568
06-30 15:32:10.661  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ | state=R schedstat=( 424041835 31308999 114 ) utm=36 stm=5 core=3
06-30 15:32:10.661  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ at android.graphics.Bitmap.nativeCreate(Native Method)
06-30 15:32:10.666  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
06-30 15:32:10.666  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
06-30 15:32:10.666  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ at android.graphics.Bitmap.createBitmap(Bitmap.java:636)
06-30 15:32:10.666  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ at com.commonsware.cwac.camera.ImageCleanupTask.run(ImageCleanupTask.java:121)
06-30 15:32:10.666  29474-29823/com.aithops.provrum.app I/dalvikvm﹕ [ 06-30 15:32:10.666 29474:29823 W/dalvikvm ]
    threadid=12: thread exiting with uncaught exception (group=0x41a33700)
06-30 15:32:10.671  29474-29823/com.aithops.provrum.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-32873
    java.lang.OutOfMemoryError
            at android.graphics.Bitmap.nativeCreate(Native Method)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
            at android.graphics.Bitmap.createBitmap(Bitmap.java:636)
            at com.commonsware.cwac.camera.ImageCleanupTask.run(ImageCleanupTask.java:121)
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
user2130951
  • 2,601
  • 4
  • 31
  • 58

1 Answers1

2

This is covered in the documentation. Either use android:largeHeap="true" or have your CameraHost return something closer to 0.0f from maxPictureCleanupHeapUsage().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Great large heap works. How would I calculate a reasonable value for maxPictureCleanupHeapUsage() ? – user2130951 Jun 30 '14 at 13:55
  • @user2130951: "How would I calculate a reasonable value for maxPictureCleanupHeapUsage() ?" -- ummm... throw darts at dart board? :-) Seriously, I don't really know. Long-term, I hope to move all the image processing into the NDK so I get off the Dalvik/ART heap in the first place. The `maxPictureCleanupHeapUsage()` is a hack for those wanting to avoid `android:largeHeap="true"`, but there's not a lot of scientific analysis behind it. And the downside is that it will mean that some devices might not clean up the images, giving you inconsistent output. – CommonsWare Jun 30 '14 at 14:00
  • I just want to added that - by using 1.0f in `maxPictureCleanupHeapUsage()`, it will gives you a properly orientated image **but** it will take 5-10 seconds before it returns, depends on the capture size. On the other hand, using 0.0f will instantly return but the image might be rotated. So, depends on if you want to handle rotation yourself, such as resize first than rotate (so less pixels to handle), or doing thumbnail rotation in foreground then full size rotation in background, to save user from waiting. – John Pang Dec 12 '14 at 08:08
  • @CommonsWare the Docs for the version 2 don't seem to have anything related to OOM (or memory at all). I'm getting an OOM in version 2 and while the largeHeap solution works, did you come up with other ways to deal with this? – Shurikn Jul 01 '16 at 07:13
  • @Shurikn: I do not know what "the version 2" is. If you mean [CWAC-Cam2](https://github.com/commonsguy/cwac-cam2), isolate the camera work [in its own process](https://github.com/commonsguy/cwac-cam2/blob/master/docs/CameraActivity.md#configuring-the-manifest-entry). If you *still* get OOM, give me [a reproducible scenario](https://github.com/commonsguy/cwac-cam2/blob/master/.github/CONTRIBUTING.md) and I can look into it. – CommonsWare Jul 01 '16 at 10:48
  • @CommonsWare I stopped testing the library after I saw that it wouldn't work for what I need, so I can't reproduce it, but here's what I remember: the app itself didn't crash, but i saw the OOM errors in the logs, and it seemed like the picture didn't work. Stopped seeing the message after changing the largeHeap setting, and the picture taking started to take more time, but worked. I can't actually confirm if the picture taking really failed the first time or not. – Shurikn Jul 02 '16 at 04:44
  • @Shurikn: "but i saw the OOM errors in the logs, and it seemed like the picture didn't work" -- OOM without a crash dialog means the attempt to rotate the image failed. You should still get a result, though, just perhaps with the EXIF orientation header still in place. OOM with a crash dialog would indicate an OOM somewhere else. – CommonsWare Jul 02 '16 at 10:47