2

So, I'm trying to write a simple camera app on android using the android.hardware.Camera API according to this guide: https://developer.android.com/guide/topics/media/camera.html

What I did so far is just creating the CameraPreview class and trying to initiate the Preview in my Main Activity, but as soon as I try to run the App on my phone it just crashes. What did I do wrong? :(

Edit: So, as far as I understand, the problem is, that my getCameraInstance() method returns a null object, as you can see in the Errorlog I posted below, because the Camera.open() fails. But why does it fail?

You can find my complete source-code on my github: https://github.com/Wursteintopf/Simple-Camera-Test

My MainActivity:

package com.example.simplecameratest;

import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

private Camera mCamera;
private CameraPreview mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCamera = getCameraInstance();

    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
}


//Method to create a Camera Instance
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    }
    catch (Exception e) {
        //blabla Camera not avaiable
    }
    return c;
}
}

And my CameraPreview.java:

package com.example.simplecameratest;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

import static android.content.ContentValues.TAG;

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
}

Edit: This is the Errorlog from my phone:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewDisplay(android.view.SurfaceHolder)' on a null object reference
    at com.example.simplecameratest.CameraPreview.surfaceCreated(CameraPreview.java:37)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:597)
    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:179)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2082)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
    at android.view.Choreographer.doCallbacks(Choreographer.java:672)
    at android.view.Choreographer.doFrame(Choreographer.java:608)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
    at android.os.Handler.handleCallback(Handler.java:742)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:5527)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Markus Vogel
  • 65
  • 1
  • 8

1 Answers1

1

Check for camera permission, if this is causing crash:

ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);

More informations about requesting permissions on runtime

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Justcurious
  • 2,201
  • 1
  • 21
  • 36
  • 1
    Well I have `uses-permission android:name="android.permission.CAMERA"` and `uses-feature android:name="android.hardware.camera"` in my Manifest, so that shouldn't be the problem. – Markus Vogel Jan 04 '18 at 16:31
  • 1
    You still have to ask for permission to use camera, it is required in android system – Mateusz Jan 04 '18 at 16:39
  • Oh, allright. But I dont really unterstand the requestPermissions Method. I assume, for the activity a simple 'this' should be sufficient, but what do I have to put in as request code? – Markus Vogel Jan 05 '18 at 09:25