Vishal Polepalli
Vishal Polepalli

Reputation: 21

Java RuntimeException "Fail to connect to camera service"

I have looked at many questions similar to mine on stackoverflow and other websites but have not been able to find a solution for my problem. I have requested the correct permissions but I still am getting this error. At first I thought it was because I was using an AVD but even while using a phone I still get this error.

MainActivity:

  package com.example.vishal.textfacialprocessing;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.qualcomm.snapdragon.sdk.face.FaceData;
import com.qualcomm.snapdragon.sdk.face.FacialProcessing;

public class MainActivity extends Activity implements Camera.PreviewCallback                        {
Camera mCamera = null;
CameraPreview preview;
private CameraPreview mPreview = null;
private int cameraside = 1;
private int Front_Camera_Index = 1;
private int Back_Camera_Index = 0;
private static boolean switchCamera;
private boolean _qcSDKEnabled;
FacialProcessing faceProc;
Display display;
private int displayAngle;
private int numFaces;
FaceData[] faceArray = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button switchCameraButton = (Button) 
      findViewById(R.id.switchCamera);
    switchCameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (cameraside == 1) {
                cameraside = 0;
            } else {
                cameraside = 1;
            }
        }
    });

    setCamera();
}

public void setCamera ()
{
    mCamera = Camera.open(cameraside);
    mPreview = new CameraPreview(MainActivity.this,mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
    mCamera.setPreviewCallback(MainActivity.this);
    _qcSDKEnabled = FacialProcessing.isFeatureSupported(FacialProcessing.FEATURE_LIST.FEATURE_FACIAL_PROCESSING);

}
 }

CameraPreview:

 package com.example.vishal.textfacialprocessing;

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

import com.qualcomm.snapdragon.sdk.face.FacialProcessing;

import java.io.IOException;

public class CameraPreview extends SurfaceView implements          SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera cameraObj;

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

    // Install a urfaceHolder.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) {
    // SurfaceHolder jholder = holder;
    // The Surface has been created, now tell the camera where to draw the      preview.
    try {
        cameraObj.setPreviewDisplay(holder);
        cameraObj.startPreview();
    } catch (IOException e) {


    }
}

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 {
        cameraObj.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 {
        cameraObj.setPreviewDisplay(mHolder);
        cameraObj.startPreview();

    } catch (Exception e){

    }
    }
    }

Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Logicat:

01-02 14:27:35.261 10635-10635/com.example.vishal.textfacialprocessing             E/Typeface: SANS_LOC file not found.
01-02 14:27:35.321 10635-10635/com.example.vishal.textfacialprocessing     E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                 Process: com.example.vishal.textfacialprocessing, PID: 10635
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vishal.textfacialprocessing/com.example.vishal.textfacialprocessing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.release()' on a null object reference
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:155)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.release()' on a null object reference
                                                                                         at com.example.vishal.textfacialprocessing.MainActivity.getCameraInstance(MainActivity.java:156)
                                                                                         at com.example.vishal.textfacialprocessing.MainActivity.onResume(MainActivity.java:58)
                                                                                         at com.example.vishal.textfacialprocessing.MainActivity.onCreate(MainActivity.java:50)
                                                                                         at android.app.Activity.performCreate(Activity.java:5958)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:155) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5696) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 

Upvotes: 1

Views: 1161

Answers (1)

TechDev
TechDev

Reputation: 31

Hi Vishal you need to add following permission in menifest file

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

If you are using the Android M permission model, you first need to check if the app has this permission during runtime, and have to prompt the user for this permission during runtime. The permission you define on your manifest will not automatically be granted on install time.

 if (checkSelfPermission(Manifest.permission.CAMERA)
    != PackageManager.PERMISSION_GRANTED) {

requestPermissions(new String[]{Manifest.permission.CAMERA},
        MY_REQUEST_CODE);
}

You will need a callback for the dialog result:

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions,      int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Now user should be able to use camera
    }
    else {
        // Your app will not have this permission. Turn off all functions 
        // that require this permission or it will force close like your 
        // original question
    }
}
}

Upvotes: 1

Related Questions