Asus Nexus 7 2013 camera.open 连接相机服务失败
Asus Nexus 7 2013 camera.open Fail to connect to camera service
我只在 Asus Nexus 7 2013 上遇到问题。所有其他设备,包括 Asus Nexus 7 2012 都可以正常工作。
此设备有 2 个摄像头 ID:0、1。
但是我无法打开相机,同时使用它们。
//...
try {
//cameraID: 0 or 1
camera = Camera.open(cameraID);
//...
} catch (Exception e) {
e.printStackTrace();
}
所以我得到:
java.lang.RuntimeException: Fail to connect to camera service
android.hardware.Camera.<init>(Camera.java:495)
android.hardware.Camera.open(Camera.java:341)
问题出在新的 Android 6+ 功能中:在运行时请求权限。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!permissionRequestInProgress) {
permissionRequestInProgress = true;
new Handler().post(new Runnable() {
@Override
public void run() {
if (ContextCompat.checkSelfPermission(RootActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(RootActivity.this,
new String[]{Manifest.permission.CAMERA},
CAMERA_PERMISSION_REQUEST);
} else {
//we got it
}
}
});
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case CAMERA_PERMISSION_REQUEST:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
//we got it
} else {
// permission denied
}
permissionRequestInProgress = false;
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
我只在 Asus Nexus 7 2013 上遇到问题。所有其他设备,包括 Asus Nexus 7 2012 都可以正常工作。
此设备有 2 个摄像头 ID:0、1。
但是我无法打开相机,同时使用它们。
//...
try {
//cameraID: 0 or 1
camera = Camera.open(cameraID);
//...
} catch (Exception e) {
e.printStackTrace();
}
所以我得到:
java.lang.RuntimeException: Fail to connect to camera service
android.hardware.Camera.<init>(Camera.java:495)
android.hardware.Camera.open(Camera.java:341)
问题出在新的 Android 6+ 功能中:在运行时请求权限。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (!permissionRequestInProgress) {
permissionRequestInProgress = true;
new Handler().post(new Runnable() {
@Override
public void run() {
if (ContextCompat.checkSelfPermission(RootActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(RootActivity.this,
new String[]{Manifest.permission.CAMERA},
CAMERA_PERMISSION_REQUEST);
} else {
//we got it
}
}
});
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case CAMERA_PERMISSION_REQUEST:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
//we got it
} else {
// permission denied
}
permissionRequestInProgress = false;
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}