Android 项目 Camera 问题:Fail to connect to camera service
·
问题与处理策略
问题描述
- 在 Android 项目中,使用相机时,报如下错误
java.lang.RuntimeException: Fail to connect to camera service
# 翻译
无法连接到相机服务
问题原因
-
通常情况是应用没有获取到相机权限,导致连接相机服务失败
-
Android 系统要求应用在使用相机之前必须获得相应的权限(声明权限 + 动态请求权限)
处理策略
- 在
AndroidManifest.xml文件中声明权限
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
- Android 6.0 及以上需要动态请求权限
private static final int PERMISSION_REQUEST_CAMERA = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_permission_test1);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "CAMERA - 权限未授予,请求权限");
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_CAMERA);
} else {
Log.i(TAG, "CAMERA - 权限已授予,执行相应操作");
doCameraOperation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CAMERA) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "CAMERA - 权限已授予,执行相应操作");
doCameraOperation();
} else {
Log.i(TAG, "CAMERA - 权限被拒绝");
}
}
}
private void doCameraOperation() {
}
- Android 6.0 及以上需要动态请求权限(Kotlin 版本)
private val PERMISSION_REQUEST_CAMERA = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_permission_test2)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED
) {
Log.i(TAG, "CAMERA - 权限未授予,请求权限");
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA),
PERMISSION_REQUEST_CAMERA
)
} else {
Log.i(TAG, "CAMERA - 权限已授予,执行相应操作");
doCameraOperation();
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_CAMERA) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "CAMERA - 权限已授予,执行相应操作");
doCameraOperation()
} else {
Log.i(TAG, "CAMERA - 权限被拒绝")
}
}
}
private fun doCameraOperation() {
}
更多推荐

所有评论(0)