I have developed a basic Android application which simply displays the phone's camera view in a VideoView
element. I have added camera access permission in the Manifest
<uses-permission android:name="android.permission.CAMERA" />
And the application consists of the main activity MainActivity.java
only. A snap of the code will be provided in the next few lines. However I face a problem when running the apk file on an actual device: An error is displayed stating that the application has stopped unexpectedly and I have no idea why.
This is the code for the main activity
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private Button StartButton = null;
private VideoView videoView = null;
private SurfaceHolder holder = null;
private Camera camera = null;
private static final String TAG = "Video";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartButton = (Button) findViewById(R.id.StartButton);
videoView = (VideoView) this.findViewById(R.id.videoView);
StartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
camera = Camera.open();
Camera.Parameters camParams = camera.getParameters();
camera.lock();
} catch(RuntimeException re){
Log.v(TAG, "Could not initialize the Camera");
re.printStackTrace();
}
}
});
holder = videoView.getHolder();
holder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
Log.v(TAG, "in surfaceCreated");
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch(IOException e) {
Log.v(TAG, "Could not start the preview");
e.printStackTrace();
}
}
The xml file basically contains two elements: the VideoView and a Start button. Any idea what stops the application unexpectedly? Is there a better way to implement this concept? Having the Start button is not necessary in my case. In fact it would be better to do this without a Start button.
I am very new to Android development and java coding. I am using Eclipse adt-bundle for Android development.