W/camerabase﹕ An Error Occurred While Connecting To Camera: 0 On Camera.open() Call
Solution 1:
You manualy uploaded your application to phone. That is why camera permission is not approved. You have to open settings->applications (or something like that) and manualy approve this permission.
Solution 2:
In Android 6, make sure you request permission for the camera. Camera access is considered one of the 'dangerous permissions'.
Solution 3:
To use the following method
android.hardware.Camera.open(int cameraId)
You should pass cameraId, If you want the front camera Id you can use the following method
privateintfindFrontFacingCamera() {
// Search for the front facing cameraint numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
cameraFront = true;
break;
}
}
return cameraId;
}
If the same camera is opened by other applications, this will throw a RuntimeException.
You must call release() when you are done using the camera, otherwise it will remain locked and be unavailable to other applications.
Your application should only have one Camera object active at a time for a particular hardware camera.
Solution 4:
make sure your app has permission for camera, e.g
<uses-permissionandroid:name="android.permission.CAMERA"/>
in AndroidManifest.xml
Solution 5:
i got the answer for this: this is for marshmallow permission issue: add this in your project:
step 1:
privatestaticfinalint REQUEST_GET_ACCOUNT = 112;
privatestaticfinalint PERMISSION_REQUEST_CODE = 200;
step2:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
step3:
privatebooleancheckPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), GET_ACCOUNTS);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
}
privatevoidrequestPermission() {
ActivityCompat.requestPermissions(this, newString[]{GET_ACCOUNTS, CAMERA}, REQUEST_GET_ACCOUNT);
ActivityCompat.requestPermissions(this, newString[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
publicvoidonRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
casePERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
boolean locationAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean cameraAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (locationAccepted && cameraAccepted)
Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access location data and camera", Toast.LENGTH_LONG).show();
else {
Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access location data and camera", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access to both the permissions",
newDialogInterface.OnClickListener() {
@OverridepublicvoidonClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(newString[]{WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}
});
return;
}
}
}
}
break;
}
}
privatevoidshowMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new android.support.v7.app.AlertDialog.Builder(CaptureActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
Post a Comment for "W/camerabase﹕ An Error Occurred While Connecting To Camera: 0 On Camera.open() Call"