user7938448
user7938448

Reputation: 68

Error with Camera

This are my permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

This is my MainActivity.java:

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
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }

    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }
}

That is my activity_main.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        />

    <Button
        android:id="@+id/button_capture"
        android:text="Capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
</LinearLayout>

And that's my CameraPreview.java:

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

import java.io.IOException;

/** 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("Error:", "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("Error:", "Error starting camera preview: " + e.getMessage());
        }
    }
}

When I open the app it crashes and this Error will come:

05-16 16:17:55.694 14553-14553/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.test.badubadu.test, PID: 14553
                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.setPreviewDisplay(android.view.SurfaceHolder)' on a null object reference
                                                       at com.test.badubadu.test.CameraPreview.surfaceCreated(CameraPreview.java:31)
                                                       at android.view.SurfaceView.updateWindow(SurfaceView.java:622)
                                                       at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:161)
                                                       at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1250)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6311)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                       at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:241)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6217)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

EDIT Error:

05-16 22:52:39.451 18993-18993/com.test.blaba.test W/CameraBase: An error occurred while connecting to camera 0: Service not available
05-16 22:52:39.451 18993-18993/com.test.blabla.test D/Error:: Error starting camera preview: Fail to connect to camera service
05-16 22:52:39.578 18993-19091/com.test.blaba.test I/Adreno: QUALCOMM build                   : b6da14b, I47548ba842
                                                                Build Date                       : 11/14/16
                                                                OpenGL ES Shader Compiler Version: XE031.09.00.03
                                                                Local Branch                     : 
                                                                Remote Branch                    : quic/LA.BF64.1.2.3_rb1.6
                                                                Remote Branch                    : NONE
                                                                Reconstruct Branch               : NOTHING

So I don't know where the Error is... Thanks for Help...:)

Upvotes: 0

Views: 907

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006539

mCamera is null, because getCameraInstance() is returning null. Most likely, that is because Camera.open() is throwing an exception. Always make sure that you can see exceptions. During early software development, that is mostly through logging those exceptions (e.g., Log.e()).

Upvotes: 2

Related Questions