Why is my Android app camera preview running out of memory on my AVD?
- by Bryan
I have yet to try this on an actual device, but expect similar results.
Anyway, long story short, whenever I run my app on the emulator, it crashes due to an out of memory exception.
My code really is essentially the same as the camera preview API demo from google, which runs perfectly fine.
The only file in the app (that I created/use) is as below-
package berbst.musicReader;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/*********************************
* Music Reader v.0001
* Still VERY under construction.
* @author Bryan
*
*********************************/
public class MusicReader extends Activity {
private MainScreen main;
@Override
//Begin activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new MainScreen(this);
setContentView(main);
}
class MainScreen extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder sHolder;
Camera cam;
MainScreen(Context context) {
super(context);
//Set up SurfaceHolder
sHolder = getHolder();
sHolder.addCallback(this);
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// Open the camera and start viewing
cam = Camera.open();
try {
cam.setPreviewDisplay(holder);
} catch (IOException exception) {
cam.release();
cam = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Kill all our crap with the surface
cam.stopPreview();
cam.release();
cam = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Modify parameters to match size.
Camera.Parameters params = cam.getParameters();
params.setPreviewSize(w, h);
cam.setParameters(params);
cam.startPreview();
}
}
}