Reputation: 2741
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)
Upvotes: 2
Views: 608
Reputation: 1006869
This is covered in the documentation. Either use android:largeHeap="true"
or have your CameraHost
return something closer to 0.0f
from maxPictureCleanupHeapUsage()
.
Upvotes: 2