I am trying to make Custom Camera app, but whenever i run my program,
getting -
Unfortunately App has Stopped
Log
please see below answer.
Custom_CameraActivity.java
public class Custom_CameraActivity extends Activity { private Camera mCamera; private CameraPreview mCameraPreview; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mCamera = getCameraInstance(); mCameraPreview = new CameraPreview(this, mCamera); FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); preview.addView(mCameraPreview); Button captureButton = (Button) findViewById(R.id.button_capture); captureButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCamera.takePicture(null, null, mPicture); } }); } /** * Helper method to access the camera returns null if it cannot get the * camera or does not exist * * @return */ private Camera getCameraInstance() { Camera camera = null; try { camera = Camera.open(); } catch (Exception e) { // cannot get camera or does not exist } return camera; } PictureCallback mPicture = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { File pictureFile = getOutputMediaFile(); if (pictureFile == null) { return; } try { FileOutputStream fos = new FileOutputStream(pictureFile); fos.write(data); fos.close(); } catch (FileNotFoundException e) { } catch (IOException e) { } } }; private static File getOutputMediaFile() { File mediaStorageDir = new File( Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp"); if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { Log.d("MyCameraApp", "failed to create directory"); return null; } } // Create a media file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss") .format(new Date()); File mediaFile; mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); return mediaFile; } }
CameraPreview.java
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mSurfaceHolder; private Camera mCamera; // Constructor that obtains context and camera @SuppressWarnings("deprecation") public CameraPreview(Context context, Camera camera) { super(context); this.mCamera = camera; this.mSurfaceHolder = this.getHolder(); this.mSurfaceHolder.addCallback(this); this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { try { mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (IOException e) { // left blank for now } } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { mCamera.stopPreview(); mCamera.release(); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { // start preview with new settings try { mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (Exception e) { // intentionally left blank for a test } } }
main.xml
Add Below Lines to your androidmanifest.xml file