I have a memory problem when using camera in my app. I have used SurfaceView for taking picture. After taking the picture in log cat it is showing Heap memory grow to 30 mb sometimes 40mb also. Because of this some times app is crashing also “OutofMemoryException”. Here I am providing my code . Please help me.
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");
}
};
I have tried force Garbage Collection, but still no use.
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
previewing = false;
}
You should inspect the memory allocations of your app, see a related post here (it also has some great articles linked at the end).