如何打开手机上的闪光灯(手电筒)?

时间:2013-06-14 18:36:44

标签: android flashlight

当你按下按钮时,有一个代码可以打开闪光灯,这就是它没有发生的原因。请帮助我理解为什么没有发生这种情况。测试了三星galaxy nexus和lg p970的应用。

的活动:

package com.example.Flashlight;

import android.app.Activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera; 
import android.hardware.Camera.Parameters; 
import android.util.Log; 
import android.view.View; 
import android.widget.*;

public class MainActivity extends Activity {

    Camera camera;
    boolean isFlashOn;
    boolean hasFlash;
    Parameters params;
    Button flashlightButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);

        checkFlashlight();  
        getCamera(); 
        initFlashlightButton();
    }

    void checkFlashlight() {
        hasFlash = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

        if (!hasFlash) {
            AlertDialog alert = new AlertDialog.Builder(
                    MainActivity.this).create(); alert.setTitle("Error"); 
            alert.setMessage("Sorry, your device doesn't support flash light!"); 
            alert.setButton("OK", new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int which) { 
                    finish();
                }
            }); 
        alert.show();
        return;
        } 
    }

    // initialization button and click listener
    private void initFlashlightButton() {
        flashlightButton = (Button) findViewById(R.id.flash_light);
        flashlightButton.setOnClickListener(new View.OnClickListener() { 
            @Override
            public void onClick(View view) {
                if (isFlashOn) turnOffFlash(); 
                else turnOnFlash();
            } 
        }); 
    }

    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
            }
        }
    }

    // here must be of the flash (it is not clear why this is not happening)
    private void turnOnFlash() {
        if (!isFlashOn) {
            if (camera == null || params == null) return;

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;
        } 
    }

    // flash OFF
    private void turnOffFlash() {
        if (isFlashOn) {
            if (camera == null || params == null) return;

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashOn = false;
        }
    }

布局:

   <?xml version="1.0" encoding="utf-8"?>    <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_height="match_parent"  android:layout_width="match_parent">

 <Button
 android:id="@+id/flash_light"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Flashlight"/>

 </RelativeLayout>

清单:

    <?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-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />

 <uses-sdk android:minSdkVersion="8"/>

 <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

 <activity android:name=".MainActivity"
 android:label="@string/app_name"
 android:theme="@android:style/Theme.Black">

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

2 个答案:

答案 0 :(得分:0)

打开相机:

    camera = Camera.open();
    Parameters p = camera.getParameters();
    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(p);
    camera.startPreview();

要关闭相机:

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

此外,你需要把它放在你的清单中:

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

答案 1 :(得分:0)

尝试将此添加到您的清单中:

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