我正在尝试用android中的CWAC库拍照。我只是在主要活动中使用一个按钮并动态创建片段。
预览会很好。但是当我点击拍摄照片时,它会因内存不足而崩溃。有人可以建议如何解决这个问题吗?
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)
答案 0 :(得分:2)
the documentation中介绍了这一点。使用android:largeHeap="true"
或让CameraHost
从0.0f
返回距离maxPictureCleanupHeapUsage()
更接近的地方。