我在同时使用Camera(低于Lollipop设备)和CameraManager类(Lollipop及以上设备)时遇到问题。
运行速度低于 Lollipop 的设备会崩溃,我认为这是由于
import android.hardware.camera2.CameraManager;
部分。
我该怎么做
package com.example.DeviceCommands;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
public class FlashController
{
Context _context;
boolean _is_supported = false;
boolean _enabled = false;
Object _camera;
@TargetApi(Build.VERSION_CODES.L) public FlashController(Context context)
{
if (context.getPackageManager().hasSystemFeature(PackageManager
.FEATURE_CAMERA_FLASH))
{
_context = context;
_is_supported = true;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
{
Camera camera = Camera.open();
if (camera == null)
{
_is_supported = false;
System.out.println("NOT SUPPORTED!");
return;
}
_camera = camera;
}
else
{
CameraManager camera = (CameraManager) _context.getSystemService(Context.CAMERA_SERVICE);
_camera = camera;
}
}
}
@TargetApi(Build.VERSION_CODES.L) public void enableFlashLight()
{
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
{
Camera camera = (Camera) _camera;
Camera.Parameters parameters = camera.getParameters();
if(!parameters.getFlashMode().equals(android.hardware
.Camera.Parameters.FLASH_MODE_TORCH)
&& !_enabled)
{
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
_enabled = true;
camera.autoFocus(new AutoFocusCallback()
{
public void onAutoFocus(boolean success, Camera camera)
{
}
});
}
}
else
{
//CameraManager manager =(CameraManager) _context.getSystemService(Context.CAMERA_SERVICE);
try
{
CameraManager camera = (CameraManager) _camera;
for(int i = 0; i < camera.getCameraIdList().length; i++)
{
System.out.println("Camera=" + camera.getCameraCharacteristics(
camera.getCameraIdList()[i]));
}
}
catch (CameraAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void disableFlashLight()
{
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP)
{
Camera cam = Camera.open();
Camera.Parameters parameters = cam.getParameters();
if(parameters.getFlashMode().equals(android.hardware
.Camera.Parameters.FLASH_MODE_TORCH))
{
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(parameters);
cam.release();
}
}
}
}
如果
import
部分会使设备的运行速度低于Lollipop崩溃,则可以正常工作吗?感谢您的帮助和建议!
编辑:
在上面添加了整个类。
Logcat输出
06-09 19:44:30.219: E/ViewRootImpl(956): sendUserActionEvent() mView == null
06-09 19:44:31.360: E/dalvikvm(956): Could not find class 'android.hardware.camera2.CameraManager', referenced from method com.example.DeviceCommands.FlashController.<init>
06-09 19:44:31.360: W/dalvikvm(956): VFY: unable to resolve check-cast 122 (Landroid/hardware/camera2/CameraManager;) in Lcom/example/DeviceCommands/FlashController;
06-09 19:44:31.360: D/dalvikvm(956): VFY: replacing opcode 0x1f at 0x003a
06-09 19:44:31.367: E/dalvikvm(956): Could not find class 'android.hardware.camera2.CameraManager', referenced from method com.example.DeviceCommands.FlashController.enableFlashLight
06-09 19:44:31.367: W/dalvikvm(956): VFY: unable to resolve check-cast 122 (Landroid/hardware/camera2/CameraManager;) in Lcom/example/DeviceCommands/FlashController;
06-09 19:44:31.367: D/dalvikvm(956): VFY: replacing opcode 0x1f at 0x0040
06-09 19:44:31.367: W/dalvikvm(956): VFY: unable to resolve exception class 120 (Landroid/hardware/camera2/CameraAccessException;)
06-09 19:44:31.367: W/dalvikvm(956): VFY: unable to find exception handler at addr 0x6b
06-09 19:44:31.367: W/dalvikvm(956): VFY: rejected Lcom/example/DeviceCommands/FlashController;.enableFlashLight ()V
06-09 19:44:31.367: W/dalvikvm(956): VFY: rejecting opcode 0x0d at 0x006b
06-09 19:44:31.367: W/dalvikvm(956): VFY: rejected Lcom/example/DeviceCommands/FlashController;.enableFlashLight ()V
06-09 19:44:31.367: W/dalvikvm(956): Verifier rejected class Lcom/example/DeviceCommands/FlashController;
06-09 19:44:31.367: D/AndroidRuntime(956): Shutting down VM
06-09 19:44:31.367: W/dalvikvm(956): threadid=1: thread exiting with uncaught exception (group=0x4100d930)
06-09 19:44:31.375: E/AndroidRuntime(956): FATAL EXCEPTION: main
06-09 19:44:31.375: E/AndroidRuntime(956): java.lang.VerifyError: com/example/DeviceCommands/FlashController
06-09 19:44:31.375: E/AndroidRuntime(956): at com.example.babymonitorer.MainActivity.onClick(MainActivity.java:121)
06-09 19:44:31.375: E/AndroidRuntime(956): at android.view.View.performClick(View.java:4421)
06-09 19:44:31.375: E/AndroidRuntime(956): at android.view.View$PerformClick.run(View.java:18190)
06-09 19:44:31.375: E/AndroidRuntime(956): at android.os.Handler.handleCallback(Handler.java:725)
06-09 19:44:31.375: E/AndroidRuntime(956): at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 19:44:31.375: E/AndroidRuntime(956): at android.os.Looper.loop(Looper.java:175)
06-09 19:44:31.375: E/AndroidRuntime(956): at android.app.ActivityThread.main(ActivityThread.java:5279)
06-09 19:44:31.375: E/AndroidRuntime(956): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 19:44:31.375: E/AndroidRuntime(956): at java.lang.reflect.Method.invoke(Method.java:511)
06-09 19:44:31.375: E/AndroidRuntime(956): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-09 19:44:31.375: E/AndroidRuntime(956): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-09 19:44:31.375: E/AndroidRuntime(956): at dalvik.system.NativeStart.main(Native Method)
MainActivity第121行:
FlashController fc = new FlashController(this);
最佳答案
我遇到过类似的问题。唯一的区别是我已将 SDK 版本更新为 Android M (API 23)。
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
而不是 Lollipop 版本。 if (context.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
CameraManager camera = (CameraManager) _context.getSystemService(Context.CAMERA_SERVICE);
}
希望这可以帮助。
关于java - 在低于Lollipop的设备上运行CameraManager,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30738502/