android - 按下切换按钮 3 次后,闪光灯 Android 应用程序崩溃,并且 LED 永远不会亮起

标签 android flash crash light

我正在尝试制作一个在 Galaxy Nexus 上运行的闪光灯应用程序。我知道您需要实现 SurfaceHolder.Callback。但是当我这样做时,应用程序正在编译和运行,没有错误。但是 LED 根本没有打开,并且应用程序在按下切换按钮 3 次后崩溃。

public class MainActivity extends Activity implements SurfaceHolder.Callback {
Camera cam;
public static SurfaceHolder mHolder;
public static SurfaceView preview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preview = (SurfaceView) findViewById(R.id.PREVIEW);
    mHolder = preview.getHolder();
    cam = Camera.open();
    try {
        cam.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton1);
    final TextView text = (TextView) findViewById(R.id.textView1);
    text.setText("Flash Light");

    toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub


            if(isChecked) {

                text.setText("Flash Light is ON");
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();

            } else {

                text.setText("Flash Light is OFF");
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
                cam.release();

            }

        }

    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mHolder = holder;
    try {
        cam.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
    cam.stopPreview();
    mHolder = null;

}
}

编辑: list
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flashlight"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.FLASHLIGHT"/> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-feature android:name="android.hardware.camera.Parameters" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:debuggable="true">
        <activity
            android:name="com.example.flashlight.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

已回答 :

我发现在我需要在我的 SurfaceView 下添加的 activity_main.xml 中的最后一行:

activity_main.xml
<SurfaceView
android:id="@+id/PREVIEW"
android:layout_width="1dip"
android:layout_height="1dip"
android:visibility="visible" />

最佳答案

检查 flashModes 是否可用。也让你Camera mCamera static否则当方向改变时,它会丢失相机实例。

public static void FlashOn(Camera mCamera)
    {
        //Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        //Check Whether device supports AutoFlash, If you YES then set AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        }
        else if(flashModes.contains(Parameters.FLASH_MODE_ON))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

    public static void FlashOff(Camera mCamera)
    {
        //Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        //Check Whether device supports AutoFlash, If you YES then set AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_OFF))
        {
             parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

关于android - 按下切换按钮 3 次后,闪光灯 Android 应用程序崩溃,并且 LED 永远不会亮起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14785504/

相关文章:

java - 在 java android 中创建对象的 JSON 数组

android - 如何在 flutter 中获取小部件的坐标、比例和旋转数据?

Android toast : appending int to string causes line break

javascript - Flash 在 IE6 和 IE7 中将自身定位在 javascript lightwindow 上的问题

android - Libgdx 应用程序在 Android 上崩溃 : Could not find class 'java.awt.Rectangle' ,

android - 如何使用 AnnotationProcessor 在带有特定包的 java(生成)目录中编写 kotlin 文件

image - 使用as3调整图像大小

android - 使用带有 flash 的 webview 时屏幕闪烁

Android 更改了 app_name 字符串,现在它崩溃了

crash - C while循环计算利息