Android模拟器相机显示闪烁的原因是由于调用相机时出现了处理器和显卡的冲突,可以通过禁用GPU渲染来解决这一问题。具体实现方式如下:
在AndroidManifest.xml文件中添加如下代码:
在调用相机时添加以下代码:
if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.M){
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
}
else {
//camera permission granted
dispatchTakePictureIntent();
}
} else {
//no need to request permissions on android devices lower than marshmallow
dispatchTakePictureIntent();
}
另外,还可以在AVD Manager的指定模拟器页面中,选择“Show Advanced Settings”并将“OpenGL ES Renderer”设置为“Angle (D3D11)”来解决相机闪烁的问题。
代码示例:
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_PERMISSION);
} else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
}