【问题标题】:RuntimeException with Samsung Galaxy -- Flashlight三星 Galaxy 的 RuntimeException -- 手电筒
【发布时间】:2014-02-12 12:52:07
【问题描述】:

我完全不知道如何实现这一点。它在我的 Razr 上运行良好,但在很多三星设备上都失败了。我有一个运行相机预览的 SurfaceView。我已经包含了一个使用手电筒的按钮。在我插入 try 块的地方抛出了异常:

if (hasFlash()) {
        btnLight = (ImageButton) findViewById(R.id.btn_light);
        btnLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Parameters p = camera.getParameters();
                if (p.getSupportedFlashModes() != null) {
                    if (p.getSupportedFlashModes().contains(
                            Parameters.FLASH_MODE_TORCH)) {
                        if (isLightOn) {
                            p.setFlashMode(Parameters.FLASH_MODE_OFF);
                            camera.setParameters(p);
                            btnLight.setImageResource(R.drawable.light_on);
                            isLightOn = false;
                        } else {
                            try {
                                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                                camera.setParameters(p);
                                btnLight.setImageResource(R.drawable.light_off);
                                isLightOn = true;
                            } catch (RuntimeException e){
                                // I don't know how to make this stupid thing work for all phones
                            }
                        }
                    }
                }
            }
        });

【问题讨论】:

  • 通常我会打印出所有相机参数并检查我可以使用的值。硬件 API 不能适用于所有设备。

标签: android parameters camera runtimeexception flashlight


【解决方案1】:

在 Android documentation 中,您可以阅读:

public void setParameters (Camera.Parameters params)

Throws
RuntimeException    if any parameter is invalid or not supported.

这意味着您使用的相机参数 (FLASH_MODE) 不受您的三星设备支持,因此无法在此型号上使用。我认为它可能根本没有内置闪光灯。

【讨论】:

  • 我不这么认为,因为它必须经过多次检查。首先检查 getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) 的方法 hasFlash。然后getSupportedFlashModes检查,然后我检查flash模式是否包含FLASH_MODE_TORCH
  • 它如何通过所有这些检查?
  • @user3029413 好的,这个RuntimeException的详细信息是什么?你有吗?
  • java.lang.RuntimeException: setParameters failed at android.hardware.Camera.native_setParameters(Native Method) at android.hardware.Camera.setParameters(Camera.java:1352) at
  • 我一直在尝试在三星的网站上查找文档,但找不到任何东西。
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多