我有一个代码
@TargetApi(23)
private void setTorchMode( CameraManager cameraManager, String id, boolean switchOn) throws CameraAccessException {
try {
final Method setTorchMode = cameraManager.getClass().getMethod("setTorchMode", String.class, boolean.class);
cameraManager.setTorchMode(cameraManager.getCameraIdList()[0],switchOn);
callbackContext.success();
} catch (IllegalArgumentException m){
} catch (Throwable t) {
callbackContext.error(t.getMessage());
}
当我尝试关闭相机时,我已经崩溃了
FATAL EXCEPTION: main
Process: uk.co.sparkenergy.androidapp2, PID: 31200 java.lang.IllegalArgumentException: Receiver not registered: android.hardware.camera2.CameraManager$1@6e1ab65
at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:793)
at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1200)
at android.hardware.camera2.CameraManager$3.run(CameraManager.java:1266)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5763)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
只有1台设备索尼e5633,请帮忙)
答案 0 :(得分:0)
public void turnOnFlashLight(){
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, true);
mTorchOnOffButton.setImageResource(R.drawable.on);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOffFlashLight() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, false);
mTorchOnOffButton.setImageResource(R.drawable.off);
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
在某些设备上,如果手电筒被禁用并且您尝试再次禁用它,则应用程序会崩溃。因此解决方案只是切换手电筒的状态。