我已经尝试了无数次让手电筒与正在使用的平板电脑一起使用的功能。应该是一种非常简单的测试应用程序,可以测试所涉及的平板电脑,所以没什么花哨的。
我尝试使用CameraManager
,我尝试使用旧版相机类,所有内容,但是无论我做什么,我都保持相同的error: "Torch for camera "0" is not available due to an existing camera user".
。即使禁用相机应用程序,这种情况仍会发生。我使用的平板电脑正在运行Android 8.1,并且是全新安装。
这是此活动的OnCreate函数:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
mcameramanager =(CameraManager)getSystemService(Context.CAMERA_SERVICE);
tryInitCamera();
}
这是tryInitCamera,它成功将相机ID设为0,并标识出闪光灯可用:
private void tryInitCamera() {
try {
String[] ids = mcameramanager.getCameraIdList();
for (String id : ids) {
CameraCharacteristics c = mcameramanager.getCameraCharacteristics(id);
Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
if (flashAvailable != null && flashAvailable
&& lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
mcameraID = id;
return;
}
}
} catch (Throwable e) {
Log.println(Log.ERROR, "CAMERA", "INITIALIZATION FAILED");
return;
}
}
这是通过其他地方的按钮打开和关闭手电筒的部分。我检查了此代码是否正在执行:
public void setFlashlight(boolean enabled) {
try {
if (mcameraID == null) {
while (mDoInit) {
Thread.sleep(1000);
tryInitCamera();
if (mcameraID != null) {
break;
}
}
}
if (mcameraID != null)
try {
mcameramanager.setTorchMode(mcameraID, enabled);
}catch (Exception e){
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
flashlight_status = (!flashlight_status);
}
权限是这样处理的:
private void requestPermission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
}catch(CameraAccessException e){
}
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
showMessageOKCancel("You need to allow access permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requestPermission();
}
}
});
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(FlashlightActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
这是清单上我拥有的所有权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.any" />
<uses-feature android:name="android.hardware.camera.flash" />
<uses-feature android:name="android.hardware.camera2.full" />
每次都会失败,并显示以下内容:mcameramanager.setTorchMode(mcameraID,enabled);
我拥有权限,并且我没有关于权限或其他任何警告。我到底能做什么?我尝试了至少两种不同的方法,我尝试了Camera.open()
-方法,但是它不断告诉我,相机不可用。 Android自己的手电筒切换效果很好。
编辑。哦,如果我使用内置的camera-app,则一切正常,即使是闪光灯也是如此。
编辑2 ,我没有尝试同时使用相机,因此,此活动正在尝试做的是打开和关闭手电筒,不需要相机预览。相机正在其他地方进行测试。