在我的应用程序中使用相机时出现内存问题。我用 SurfaceView 拍照。在 log cat 中拍照后,它显示堆内存增长到 30 mb,有时也增长到 40mb。因此,有时应用程序也会崩溃“OutofMemoryException”。在这里我提供我的代码。请帮我。
surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
capture.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
camera.takePicture(myShutterCallback, myPictureCallback_RAW,
myPictureCallback_JPG);
capture.setClickable(false);
}
});
PictureCallback myPictureCallback_RAW = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
System.out.println("myPictureCallback_RAW");
}
};
PictureCallback myPictureCallback_JPG = new PictureCallback() {
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
try {
bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0,
arg0.length);
}
catch (OutOfMemoryError e) {
Toast.makeText(FromCamera.this, "Please try again",
Toast.LENGTH_SHORT).show();
}catch (Exception e) {
Toast.makeText(FromCamera.this, "Please try again",
Toast.LENGTH_SHORT).show();
}
capture.setEnabled(false);
buttonBack.setVisibility(View.VISIBLE);
buttonDone.setVisibility(View.VISIBLE);
buttonBack.setEnabled(true);
buttonDone.setEnabled(true);
}
};
ShutterCallback myShutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
System.out.println("onShutter");
}
};
我试过强制垃圾收集,但仍然没有用。
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
previewing = false;
}