0

I am using Camera for Barcode scanner App and On Some devices(LG G Flex, Asus Nexus 7) getting : Android Run time Exception -Failed to Connect Camera Service. Here is the snippet from menifest file below :

`uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.CAMERA"
....`

I am releasing camera on pause, stop and on destroy.

    /** 
* Restarts the camera. 
*/ 
@Override 
protected void onResume() { 
super.onResume(); 
try { 
startCameraSource(); 
} catch (Exception e) { 
e.printStackTrace();

    }
}

/**
 * Stops the camera.
 */
@Override
protected void onPause() {
    super.onPause();
    if (mPreview != null) {
        mPreview.stop();
    }
}

/**
 * Releases the resources associated with the camera source, the associated detectors, and the
 * rest of the processing pipeline.
 */
@Override
protected void onDestroy() {
    super.onDestroy();
    if (mPreview != null) {
        mPreview.release();
    }

}

Still getting above runtime exception, and i do not have above two devices so that i can reproduce. Is there any solution of that issue?

Nikky
  • 21
  • 1

1 Answers1

0

Hi 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
    }
}
}